Print hexadecimal code from int into string - c#

I need to save a string with the hexadecimal code of an int.
For example the hexadecimal value for the int 15 is xoooF.
My problem is that I have to save this code in a string like this:
int myint = myStringLength; //this value might change
string myIntExhCode = myint.convertThisIntoMyCode();
//and the string has to be exactly "/xoooF"
So in this question I have two problmes:
The first is how to automatically convert an int into the hexadecimal code like 15 = xoooF
The second is how to create a string containing \xoooF because any try of concatenating strings like this resulted into a \\xoooF and this is not correct since in output I need the string to be converted into the ascii code.
How can I achieve those two tasks?
Any help will be appreciated

Your question is quite vague. If you want hexadecimal format, but with 0 (digit zero) changed into o (small latin letter o) you can implement, say, an extension method (in order to keep your proposed code intact):
public static partial class IntExtensions {
public static string convertThisIntoMyCode(this int value) {
return "\\x" + value.ToString("X4").Replace('0', 'o'); // or "/x" + ...
}
}
...
int myint = myStringLength; //this value might change
string myIntExhCode = myint.convertThisIntoMyCode();
// Test output
Console.Write(myIntExhCode);

How about
int i = 15;
string result = "\\x" + i.ToString("X").PadLeft(4, 'o');

Related

why does parse function return o?

I am new to c# programming and I recently bumped into one problem which looks pretty basic.I store the string value like SV_1 in the variable lastServiceNo and split it using Split function and the result is stored in string array called index.Basically index[1] has some numeric value bt as string. now I want to convert string into int. In the following code , it behaves as expected until parse function is encountered.I could not understand why does this parse function returning 0 as index[1] has some numeric value in it. Can somebody point the problem please??
public string GenerateServiceNo() {
DataAccessLayer.DataAccessLayer dlObj= new DataAccessLayer.DataAccessLayer();
string lastServiceNo = dlObj.GetLastServiceNo();
string[] index = lastServiceNo.Split('_');
int lastIndex = int.Parse(index[1]);
return "SV_"+(lastIndex++).ToString();
}
int.Parse(string s) throws an exception if the number is too bug in terms of data size or the string "s" is not in the correct numerical format.
The format that this method accepts is "[ws][sign]number[ws]" where:
[ws] is optional for one or more whitespace(" ")
[sign] is optional for "+" or "-"
Check here for the full reference.
Thus said, I can assure you that if int.Parse(index[1]) returns 0 then that means index[1] equals "[ws][sign]0[ws]" using the transcript above.
However, looking at your code, I can conclude that you're incrementing a local variable after assignment without using its incremented value afterwards. Perhaps you meant that this operation shouldn't be 0?
If that's the case then I believe this is what you're trying to achieve:
public string GenerateServiceNo()
{
DataAccessLayer.DataAccessLayer dlObj= new DataAccessLayer.DataAccessLayer();
string lastServiceNo = dlObj.GetLastServiceNo();
string[] index = lastServiceNo.Split('_');
int lastIndex = int.Parse(index[1]);
return string.Format("SV_{0}", ++lastIndex);
}
Assuming index[1] == "0", this method will now return "SV_1".

Specify decimal places using variables inside string interpolation

I have a string format which includes two integer variables, each of which needs to be formatted to a variable length:
int x = 1234;
int y = 42;
// Simplified, real values come from method outputs, so must use the variables:
int xFormatDigitCount = 7;
int yFormatDigitCount = 3;
var xStringFormat = new string('0', xFormatDigitCount); // "0000000"
var yStringFormat = new string('0' ,yFormatDigitCount); // "000"
For now I only managed to get the desired format using the integer variables' .ToString() methods:
var xString = x.ToString(xStringFormat);
var yString = y.ToString(yStringFormat);
return $"{xString}-{yString}";
But this seems like an overhead since string interpolation supports the format {var:format}. Is there a way to get my string with only string interpolation, without using x and y's ToString()?
I'm not sure I understand the question, but format specifiers for string.Format and, thus, string interpolation are textual - they don't accept variables.
You either use static format specifiers:
$"{x:0000000}-{y:000}"
Or resort to the good old string.Format:
string.Format(
$"{{0:{new string('0', xFormatDigitCount)}}}-{{1:{new string('0', yFormatDigitCount)}}}",
x,
y);
Edit:
Based on weston's answer:
$"{x.ToString($"D{xFormatDigitCount}")}-{y.ToString($"D{yFormatDigitCount}")}"
Is there a way to get my string with only string interpolation, without using x and y's ToSTring()
I don't believe so, but it can be so much cleaner thanks to ToString("Dx"):
All in one (nested interpolations):
public string Format(int x, int y, int xDigitCount, int yDigitCount)
{
return $"{x.ToString($"D{xDigitCount}")}-{y.ToString($"D{yDigitCount}")}";
}
Stack Overflow syntax highlighting can't keep up, so it looks odd, but this is how it looks in VS:
You can just call the ToString method within the interpolated string.
$"{x.ToString(xStringFormat)}-{y.ToString(yStringFormat)}"

converting double/floating to integer in C#

My question might looks like silly, but i struck with it.
I have a string value "155.300" and i want to convert it to integer.
I tryed but throwing System.FormatException....pls someone help me out.
Since your source data is string you need to Convert it to Double first then just cast it to int or use Convert.ToInt32, but remember Convert.ToInt32 rounds it to nearest integer number, whereas casting takes the int part of the number (truncate)
double d = Convert.ToDouble("155.00");
int a = (int) d;
int b = Convert.ToInt32(d);
Or in a single Line
int b =(int) Convert.ToDouble("155.000");
EDIT
Since you want to use decimal point as thousand separator, I believe in German culture you can try the following.
int b = ((int)Convert.ToDouble("154.500", new CultureInfo("de-DE")));
That will give you 154500
EDIT 2
Or much better is to use int.Parse with NumberStyles.AllowThousands:
int b = int.Parse("154.500", NumberStyles.AllowThousands, new CultureInfo("de-DE"));
First parse it as a decimal or double (probably best to use decimal as you've got decimal data) then either cast or use something like Math.Round, depending on your requirements.
Basically, you need to always consider what data you've got: "155.300" isn't a string representation of an integer, so don't try to parse it as an integer. Parse it as what it is, then convert that to an integer.
Alternatively, you could hack at the string representation first, but personally I find that to be a more brittle approach in many cases.
EDIT: Note that if this is really already an integer, but with a thousands separator, you don't need to use double at all - you can use int.Parse, specifying an appropriate culture and number style:
int parsed = int.Parse(text, NumberStyles.Integer | NumberStyles.AllowThousands,
culture);
Here is a working conversion sample. Take a special look with the edge conditions, the output may be different if using several rounding/casting techniques
class Program
{
public static int MyToInt(string str)
{
double result;
bool success = Double.TryParse(str, out result);
if (!success)
{
throw new ArgumentException(
"Cannot parse a string into a double");
}
return Convert.ToInt32(result); // 156
//return (int)result; // 155 <<
//return (int)Math.Round(result); // 156
}
static void Main(string[] args)
{
string s = "155.500";
int value = MyToInt(s);
}
}
You can try this:
string str = "123.123";
str = str.Remove(str.IndexOf('.'), 1);
int result;
int.TryParse(str, out result);
Edit: Based on your comment, modified to multiply by thousand.
Or you can just try:
string str = "123.123";
double result;
double.TryParse(str, out result);
int final = (int)(result * 1000);

Casting using a decimal in C#

private int FindNumber(string sPar)
{
// Get the last number
int len = sPar.Length;
string s = sPar.Substring(len-1);
return new (int)Decimal(s);
}
In this I am getting the error ; required . Can any ine help me on this.
Thank You!
Change your code to this:
private int FindNumber(string sPar)
{
int len = sPar.Length;
string s = sPar.Substring(len - 1);
return Convert.ToInt32(s);
}
Or, even shorter:
private int FindNumber(string sPar)
{
return Convert.ToInt32(sPar.Substring(sPar.Length - 1));
}
I'm not 100% what you are trying to do here, but if you want to get 4 as the result from the string "17894" i guess you want to write it like this with minimum number of changes:
private int FindNumber(string sPar) {
// Get the last number
int len = sPar.Length;
string s = sPar.Substring(len-1);
return int.Parse(s);
}
No reason to include a decimal and parse it to an int if you are only taking one char of the string anyway.
Note that this will give an exception if the last char of the string for any reason is not a number.
what is Decimal(s)? If you mean "parse as a decimal, then cast to int":
return (int)decimal.Parse(s);
If it is known to be an integer, just:
return int.Parse(s);
Actually, since you are only interested in the last digit, you could cheat:
private static int FindNumber(string sPar)
{
char c = sPar[sPar.Length - 1];
if (c >= '0' && c <= '9') return (int)(c - '0');
throw new FormatException();
}
Decimal(s) is not a valid call since Decimal is a type. Try Decimal.Parse(s) instead if you are certain that s is a valid decimal, stored as a string. If not, use Decimal.TryParse instead.
Please take into account different Culture related problems also, check out the overload that takes an IFormatProvider (I think, not sure about the exact name)
Do you just want to parse the digit from the last position in the string?
private int FindNumber(string sPar)
{
return int.Parse(sPar.Substring(sPar.Length - 1));
}
Note that this will fail if (a) the string is null, (b) the string is empty, or (c) the last character of the string isn't a digit. You should add some checking to your method if these situations are likely to be a problem.
The last line is completely wrong.
Your code takes the last char of a string(that i presume is always a number) and cast it to an int.
Do the following
try{
return Int32.Parse(s);
}
catch(Exception e)
{
// manage conversion exception here
}
I suppose you want to convert string to decimal as your code is not very clear.
you can use
Decimal d1;
if(Decimal.TryParse(sPar,out d1))
{
return d1
}
else
{
return 0;
}

C# Converting a string containing a floating point to an integer

What is the best way to take a string which can be empty or contain "1.2" for example, and convert it to an integer? int.TryParse fails, of course, and I don't want to use float.TryParse and then convert to int.
Solution 1: Convert.ToDouble (culture-dependent)
You may using Convert.ToDouble. But, beware! The below solution will work only when the number separator in the current culture's setting is a period character.
var a = (int)Convert.ToDouble("1.2");
Solution 2: Convert.ToDouble (culture-independent)
It's preferable to use IFormatProvider and convert the number in an independent way from the current culture settings:
var a = (int)Convert.ToDouble("1.2", CultureInfo.InvariantCulture.NumberFormat);
Solution 3: Parse & Split
Another way to accomplish this task is to use Split on parsed string:
var a = int.Parse("1.2".Split('.')[0]);
Or:
var a = int.Parse("1.2".Split('.').First());
Notes
If you want to handle empty and null strings, write a method and add string.IsNullOrEmpty condition.
To get decimal separator for the current culture setting, you can use NumberFormatInfo.NumberDecimalSeparator property.
You should also keep eye on rounding to avoid traps.
Select casting, Parse, TryParse or Convert class wisely. Read more at:
How to: Convert a string to an int (C# Programming Guide)
How to: Determine Whether a String Represents a Numeric Value (C# Programming Guide)
I don't know what's wrong with parsing to a float and converting to an int. I doubt that any other way would be more efficient but here's an attempt:
//allows empty strings and floating point values
int ParseInt(string s, bool alwaysRoundDown = false)
{
//converts null/empty strings to zero
if (string.IsNullOrEmpty(s)) return 0;
if (!s.Contains(".")) return int.Parse(s);
string parts = s.Split(".");
int i = int.Parse(parts[0]);
if (alwaysRoundDown || parts.Length==1) return i;
int digitAfterPoint = int.Parse(parts[1][0]);
return (digitAfterPoint < 5) ? i : i+1;
}
In order to globalize the code you would need to replace "." with System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.
int a = (int)Math.Round(float.Parse("0.9"));
You need to round it first unless you want 0.9f being converted to 0 instead of 1.
Maybe you can try to delete everything after floating point using string functions and then convert to int. But seriously I don't think it's better than converting to float and then to int.
I think another way of doing it would be splitting the string into pieces taking the decimal (.) as the delimiter and then parsing for the integer. Of course, I am yet to ask you if the string might contain values like "37.56 miles in 32.65 seconds" type values.
Considering there will be only one value (string or number) in the string, I can think of something in the following line:
public int64 GetInt64(string input)
{
if (string.IsNullOrEmpty(input)) return 0;
// Split string on decimal (.)
// ... This will separate all the digits.
//
string[] words = input.Split('.');
return int.Parse(words[0]);
}
You can use the Visual Basic runtime Library to accomplish this from c#.
You need to add a reference to the assembly Microsoft.VisualBasic.dll to your solution.
Then the following code will do your conversion:
using VB = Microsoft.VisualBasic.CompilerServices;
class Program
{
static void Main(string[] args)
{
int i = VB.Conversions.ToInteger("1.2");
}
}
I had this same problem and ended up using a hybrid of Mark's and Dariusz':
if (num == "")
{
num = "0.00";
}
var num1 = (float)Convert.ToDouble(num);

Categories

Resources