Convert Hexa Decimal to to Decimal
-
Hi, can any one tell me how to Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)in C#.Is there any direct function to do this? Thanks in advance
-
Hi, can any one tell me how to Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)in C#.Is there any direct function to do this? Thanks in advance
Cheers, Vikram.
Current activities: Films: The classic Pink Panther series TV series: Friends, season 3 Books: Liar's Poker, by Michael Lewis.
Carpe Diem.
-
Hi, can any one tell me how to Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)in C#.Is there any direct function to do this? Thanks in advance
Member 3057887 wrote:
Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)
Why would you want to convert a hex value to a float? Unless it is the hex representation of the internal value of a float? Or are you just to lazy to tell us what you want to achieve? Give information, including what you have tried, and you are more likely to get a helpful response.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Hi, can any one tell me how to Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)in C#.Is there any direct function to do this? Thanks in advance
-
String hexNumber = "4363A00E"; int i = Int32.Parse(hexNumber, System.Globalization.NumberStyles.HexNumber); Why do you want to display it in decimal? You can't create decimal with an hexa...
Hi, Thanks for replying so soon. I am doing an application involving serial Port.I am getting data from Serial Port in Hexa decimal format(for eg.436300E).I need to convert that into floating point(eg.123.34). Thanks
-
Hi, Thanks for replying so soon. I am doing an application involving serial Port.I am getting data from Serial Port in Hexa decimal format(for eg.436300E).I need to convert that into floating point(eg.123.34). Thanks
Data from a serial port generaly comes in as a stream of bytes, and are not amenable to conversion to float directly - parse probably wont work. I assume your data is coming from a data logger or sillyscope or similar? You will have to give more detail on the incomming data stream before anyone can help you much.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Hi, Thanks for replying so soon. I am doing an application involving serial Port.I am getting data from Serial Port in Hexa decimal format(for eg.436300E).I need to convert that into floating point(eg.123.34). Thanks
Hexidecimal notation usually is always involved with integer values. To convert to a float, you'd need to convert to an integer first and then move into the floating point world. Your hexidecimal representation, is it fixed point or floating point?
Regards, Rob Philpott.
-
Cheers, Vikram.
Current activities: Films: The classic Pink Panther series TV series: Friends, season 3 Books: Liar's Poker, by Michael Lewis.
Carpe Diem.
you really need to see this website. www.lmgtfy.com . It's much more informative
-
Hi, Thanks for replying so soon. I am doing an application involving serial Port.I am getting data from Serial Port in Hexa decimal format(for eg.436300E).I need to convert that into floating point(eg.123.34). Thanks
In that case what you need to do is use the
BitConverter
which will convert a set of bytes into another data type. To get your bytes (hopefully 4 of them) to a float you need to useToSingle
So you'd have:byte[] data = ...
float myFloat = BitConverter.ToSingle(data)ToSingle
also takes a second parameter (all of the functions in BitConverter do). The second parameter is the offset in the set of bytes where your float starts. That way if you had a stream you could just read all of the bytes out and then work your way through; rather than copying out 2 or 4 bytes to some other array all the time. EDIT: You say your getting the data in hex format, I assume your just getting bytes which are usually represented as hex. If you are actually getting a string from the port then you'll need to convert that back into a set of bytes,.My current favourite word is: Delicious!
-SK Genius
-
Hi, can any one tell me how to Convert a hexa Decimal value(4363A00E) to decimal(in the format of 123.34567)in C#.Is there any direct function to do this? Thanks in advance
You may use a 'union' see [^]
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Explicit)]
struct TestUnion
{
[System.Runtime.InteropServices.FieldOffset(0)]
public UInt32 i;[System.Runtime.InteropServices.FieldOffset(0)]
public Single s;
}and then, for instance:
TestUnion tu = new TestUnion();
tu.i = UInt32.Parse("4363A00E", System.Globalization.NumberStyles.HexNumber);
Single s = tu.s;However it gives
227.625214
as result (are you sure about the expected output?). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]