First time here? Check out the FAQ!
THIS IS A TEST INSTANCE. Feel free to ask and answer questions, but take care to avoid triggering too many notifications.
0

Interpreting floating point numbers from hex values

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?

Wirey's avatar
1
Wirey
asked 2022-12-01 11:02:21 +0000
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

0

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.)

Guy Harris's avatar
19.9k
Guy Harris
answered 2022-12-01 11:44:36 +0000
edit flag offensive 0 remove flag delete link

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é's avatar André (2022-12-01 19:35:34 +0000) edit
add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer