from the Forum department...
Converting Hex to Floating point
Software, including programming, OS issues, etc. topic
Posted by Saeid Yousefpour on 5 September, 2009 - 11:05 am
Hi all.

I get a hex value from a device through serial port over modbus protocol. but I don't know how to convert it to equivalent fraction decimal.
for example:
I get : 41C41DC3
and it is : 24.51453

is any body familiar with algorithm?
also if there is a algorithm to convert binary to fraction decimal it would help.

or if there is any function to do that?

Thanks : Saeid_Yousefpour [at]

Posted by Bruce Durdle on 5 September, 2009 - 9:12 pm
It looks as if the value is encoded in IEEE 32-bit floating-point format.

The first bit is a sign bit - 1 = -, 0 = +

The next 8 bits are the exponent with a offset of 127 In your case
41C = s100 0001 1nnn and the exponent = 131 - 127 = 4.

The remainder is a normalised value between 1 and 2 - 01 and 10 in binary.

Since the first bit is always 1 it is always present and is not transmitted.

So the remaining bits are the binary fraction part - the first bit is worth 0.5, the 2nd bit 0.25, the 3rd 0.125 and so on.

Just looking at the first few bits, the value is (.)100 0100 0001 1100 = 0.5 + 0 x 0.25 + 0 x 0.125 + 0x 0.0625 + 1 x .03125 = 0.5 + .03125 = 0.53125

Add this to the 1 and the mantissa part is 1.53125. Multiply this by 2 to the power of the exponent = =2^4 = 16 and you get 1.53125 x 16 = 24.5 to 3 sig figs. Add in the rest of the bits and you'll get something approaching your value.

To recap,
The first bit is a sign bit s.

The next 8 bits represent 127+ n where n is the power of 2 to multiply by.

The mantissa is 1.bbb bbbb bbbb bbbb bbbb bbbb where each succeeding \b is weighted at 1/2 the value of the one before. The value represented is s 1. bbb bbbb bbbb bbbb bbbb bbbb x 2^n.

Hope this helps.

Posted by Saeid Yousefpour on 6 September, 2009 - 3:21 pm
Thank you Bruce. it works very good.

Posted by M Griffin on 5 September, 2009 - 10:56 pm
That is a single precision floating point number in "big-endian" (network order) byte order. I don't know if you can do this in VB DotNet, but in Python this is very easy.

Typing the following:
struct.unpack('!f', '\x41\xc4\x1d\xc3')

results in:

As for binary to floating point, that is what the above is doing. The "hex" number is just a convenient way of representing a binary number in a printable format.

Posted by pvb on 6 September, 2009 - 2:00 am
I suppose the format is IEEE_754-1985

It might be necessary to reverse the byte order depending on which type of CPU is used.

Posted by kevin grey on 6 September, 2009 - 10:42 am
Hi, There are a lot of examples and information about floating point number's.

I have created a simple vbscript that you should beable to use;

'number to be converted
'HexNumber = 1103371715 'as a decimal
HexNumber = &h41C41DC3 'as hex

'work out exponent get bits 23-30
exponent = HexNumber
For n = 0 To 22
exponent = exponent / 2
exponent = exponent - 127
exponent = 2 ^ Int(exponent)
'Debug.Write Int(exponent) & vbcrlf

'work out Mantissa bits 0-22
'check each bit inturn and store result
decimalpart = 1
mantissa = 1
For n = 22 To 0 Step -1
decimalpart = decimalpart / 2
testbit = 2 ^ n
If (Hexnumber And testbit) Then
'Debug.Write testbit & " " & n & vbCrLf
mantissa = mantissa + decimalpart
End If

Result = exponent * mantissa

MsgBox Result


Posted by rana on 4 June, 2012 - 8:04 am
1-> reverse it like this C31DC4412
2-> convert it into binary its in ascii Hex
3-> then just assign the value to a float variable

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2014 Nerds in Control, LLC. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, MySQL and Apache. Be happy.

While money doesn't buy love, it puts you in a great bargaining
Advertise here
our advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive