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);
Related
This question already has answers here:
problem converting 4-bytes array to float in C#
(3 answers)
Closed 9 months ago.
I have a byte array of four bytes which contains the byts of a FLOAT values.
For example
array[0]=0x1F
array[1]=0x05
array[2]=0x01
array[3]=0x42
this should be 0x4201051f, which means 32.255 value.
Do you have any suggestion on how to group the array and save the value in a float data?
Thank you
As suggested by shingo, the linked question provides the answer:
var input = new byte[] { 0x1F, 0x05, 0x01, 0x42 };
Console.WriteLine(BitConverter.ToSingle(input, 0)); // Outputs 32.255
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:
Convert int to a bit array in .NET
(10 answers)
Closed 7 years ago.
I want to convert my base 10 number to base 2 and then store parts in an array.
Here are two examples:
my value is 5 so it will be converted to 101 then I have an array like this: {1,0,1}
or my value is 26 so it will be converted to 11010 then I will have an array like this: {0,1,0,1,1}
Thank you in advance for your time and consideration.
To convert the int 'x'
int x = 3;
One way, by manipulation on the int :
string s = Convert.ToString(x, 2); //Convert to binary in a string
int[] bits= s.PadLeft(8, '0') // Add 0's from left
.Select(c => int.Parse(c.ToString())) // convert each char to int
.ToArray(); // Convert IEnumerable from select to Array
Alternatively, by using the BitArray class-
BitArray b = new BitArray(new byte[] { x });
int[] bits = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();
Source: https://stackoverflow.com/a/6758288/1560697
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.
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.