This question already has answers here:
How to parse hex values into a uint?
(4 answers)
Closed 7 years ago.
I have a string varaible str. This variable has a value "26". Can I convert it into hex that results 0x26 not 0x1A.
Means str="26"
int iConvert=**SomeConvertionFunction(str)**
should result into iConvert=0x26
int hex = int.Parse("26", NumberStyles.HexNumber);
hex value will be 38 which is the decimal representation of hex number 0x26.
If I understand your question basically you want to store the Hex value of the string in a int?
having the value 26 or 0x1A is the same.
To convert the int to a string use int.ToString('X');
And please not that 26 in base 10 in equal to 0x1A in base 16.
Related
This question already has answers here:
Convert integer to binary in C#
(22 answers)
Closed 3 years ago.
By using that statement I can change my binary value to decimal value by swapping the values but why isn't this statement working for decimal to binary conversion?
int decimalVal = 10;
int binaryVal = Convert.ToInt32(decimalVal, 2); // want 1010
You should use Convert.ToInt32(String, Int32) method to convert binary to integer and Convert.ToString(Int32, Int32) method to convert integer to binary.
string binaryVal = Convert.ToString(decimalVal, 2);
To find more information:
https://learn.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=netframework-4.8
https://learn.microsoft.com/en-us/dotnet/api/system.convert.tostring?view=netframework-4.8
int decimalVal = 10;
string binary = Convert.ToString(decimalVal, 2);
They are of different byte sizes: https://condor.depaul.edu/sjost/nwdp/notes/cs1/CSDatatypes.htm And you are trying to convert one directly and expect binary while this does not work like that.
This two answers should help you:
How do I convert a decimal to an int in C#?
And
Convert integer to binary in C#
What you will figure out from this two answers is that Covnert.ToInt32 does not have an overload that takes an instance of decimal and converts it to binary. You will need to cast the decimal first to an integer and then you would be able to cast it to binary, For example:
decimal value = 8;
int n = Convert.ToInt32(value);
string binary = Convert.ToString(n, 2);
binary.Dump();
The output would be: 1000
This example is tested in Linqpad
This question already has answers here:
char + char = int? Why?
(15 answers)
Closed 4 years ago.
This is something they included as part of my course, just wondering why it does this and what they were trying to show with it but can't seem to figure it out. Is it some sort of principle when trying to concatenate chars to numbers?
Am I right in assuming that 'z' is a char because it's in single quotes here?
Is it some sort of error because you shouldn't write stuff like this?
Thanks in advance!
z is char value, char is basically a number. z will be implicitly converted to int (z code is 122), that's why 3 + 'z' + 4 == 129. It will be converted to int because in statement 3 + 'z' 3 is int, so result of addition will be also int.
In C# char is a 16 bit numeric value which represents a unicode character. So in your case z is implicitly evaluated as 122. So 3 + 122 + 4 equals 129.
This question already has answers here:
Convert Hex to Double
(4 answers)
Closed 6 years ago.
i have hex data "44 34 00 00" , and i'll Convert to double "720.0" using C#
like this..
anyone can help? thanks..
What you have is the bytes from a Single (aka float), so do this:
double result = BitConverter.ToSingle(BitConverter.GetBytes(0x44340000), 0);
Console.WriteLine(result); // Prints 720
check that
https://msdn.microsoft.com/en-us/library/system.bitconverter.int64bitstodouble
BitConverter example:
double d = BitConverter.Int64BitsToDouble(0xdeadbeef);
This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
Closed 8 years ago.
This is a small snippet of the code I have written in Visual Studio.
I need to convert the int add into an hex value (later into a string) as I am accessing the memory of a uC. But when I view the variable during debugging it displays it as a string "add16". What could I be doing wrong?
for (int add = 0; add <= 0xfffff; )
{
for (int X = 0; X <= 15; X++)
{
string address = add.ToString("add16");
addr = Convert.ToString(address);
port.WriteLine(String.Format("WRBK:{0}:{1}", addr, parts[X]));
add++;
}
}
There is a simple and very convenient method that takes an integer and returns a representation of it as a string in hexadecimal notation
string address = Convert.ToString(add, 16);
So, perhaps, your internal loop could be rewritten as
port.WriteLine(String.Format("WRBK:{0}:{1}", Convert.ToString(add++, 16), parts[X]));
and using the standard numeric format strings specifiers reduced to
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add++, parts[X]));
You can tell .NET's String.Format to use hex output for the integer, eliminating any code to hex conversion, use:
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add, parts[X]));
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have 2 input int values that need do be divided and sen back as a string with 6 decimals.
int a = 240
int b = 1440.
I want to divide them and send back a text string with 0,166667
I have tried many code examples but none have worked.
You need to convert at least one to a decimal value:
double result = (double)a / b;
or
decimal result = (decimal)a / b;
On "decimal vs. double" see THIS question.