Ask Your Question
0

Interpreting floating point numbers from hex values

asked 2022-12-01 11:02:21 +0000

Wirey gravatar image

A third-party vendor is sending data via TCP using a program written in C to a listening program we've developed in C# on another server. It's mostly working okay but sometimes there seems to be misalignment in the data which I've been trying to isolate by interrogating the data stream using Wireshark.

In certain packets I know there are some 4-byte integers and some 4-byte reals (floating point numbers). Taking the hexadecimal value 8d:02:00:00 from Wireshark which I know represents an integer I can enter 28D into the Windows Calculator in Programmer mode and it tells me this is the decimal number 653. But if I take 94:0e:4b:3f which I know represents a real, how can I determine its decimal value?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2022-12-01 11:44:36 +0000

Guy Harris gravatar image

A 4-byte floating-point number could be in a number of formats, but the most likely format, at this point, is probably IEEE 754 binary format.

If 8d:02:00:00 is 653 rather than 2365718528, that means that the numbers are little-endian. A little-endian 94:0e:4b:3f, in binary, would be 111111010010110000111010010100. See the Wikipedia article linked to in order to see that as a IEEE 754 binary32 value.

(There may be tools that will do that for you, but I don't know whether there are any.)

edit flag offensive delete link more

Comments

I don't know about tools for this, but in C code filling a float and then print it would be very simple to do. However for a quick one-liner using Perl is easier. For example:

perl -e 'print unpack "f", pack "H*", "940e4b3f";'

Use reverse pack to fix endianness if necessary.

Converting a float into 4 byte hex string can also be done:

perl -e 'printf "%08x", unpack "L", pack "f", 70.5;'

unpack "N" for network byte order.

André gravatar imageAndré ( 2022-12-01 19:35:34 +0000 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2022-12-01 11:02:21 +0000

Seen: 85 times

Last updated: Dec 01 '22