Today is...
Saturday, July 30, 2016
Welcome to Control.com, the global online
community of automation professionals.
Featured Video...
EtherCAT with CTC’s master lets your multivendor network play well together...
Help keep our servers running...
Converting Hex to Floating point
how to convert hex to floating point number?
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 vb.net function to do that?

Thanks : Saeid_Yousefpour [at] yahoo.com

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.
Cheers,
Bruce.

By Saeid Yousefpour on 6 September, 2009 - 3:21 pm

Thank you Bruce. it works very good.

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:
(24.514532089233398,)

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.

I suppose the format is IEEE_754-1985

http://en.wikipedia.org/wiki/IEEE_754-1985#Single-precision_32-bit

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

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
Next
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
Next

Result = exponent * mantissa

MsgBox Result

Kevin

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