MATLAB Answers

Convert 32 bit HEX to signed floating point

229 views (last 30 days)
So far I use the function hexsingle2num to convert a double type number (i.e. 41c1999a = 24.200000762939453) but when I want to use the same to convert ffffffff it gives me NaN, then I have to manually use nhex2dec('ffffffff',32) = -1 (correct answer),same with another example C0728F5C = -3.79. What I am looking for is a way to automatise this process since I have thousands of values. Is there a function in matlab that includes the conversion of HEX to SIGNED floating point?

Accepted Answer

Maitreyee Mordekar
Maitreyee Mordekar on 13 Nov 2017
Edited: KSSV on 10 Dec 2019
By hexsingle2num function I assume that you are referring to the following File Exchange submission:
This is one of the several submissions in MATLAB File Exchange on MATLAB Central which is a forum for our product users to interact, exchange information and knowledge, without MathWorks' involvement. You may contact the author of this submission directly for specific questions about the implementation.
However, you can obtain the same results using the following function:
Also, if you still need a way to convert hexstring to double precision number then you can consider using hex2num:
Hope that helps!
Walter Roberson
Walter Roberson on 4 Feb 2021
If you have a stream of bytes intended to represent a single precision number, then you need to know the order the bytes are in.
The international standard for network transmission that does not specifically specify otherwise, is that the most significant byte should be first (that is the byte that includes the sign and exponent) and the least significant byte should be last. This is known as "big-Endian" order, or more formally as "network byte order"
It is not, however, the order used by Intel x86 or x64 architecture, which puts the least significant byte first.
dec2hex and num2hex output in big-Endian, most significant byte first. But that is not the memory order of Intel.
This matters because when you typecast() bytes to single precision, the order used is memory order of the host system. '01020304' as output from num2hex would have 01 in the most significant byte, but typecast(uint8([1,2,3,4]),'single') would have the 01 in the least significant byte. typecast does not automatically adjust between big-Endian and little-Endian.
Because of these factors, you may need to use swapbytes() on the single() after the typecast.
If your source of data is a program designed for Windows, then a lot of the time they never bothered to think about byte order and just emit binary that is little-Endian and that often does not need to swapbytes for use on Intel architecture.
By the way, matlab .mat files contain a header that tells matlab which order the file was written in, so matlab automatically adjusts data read from mat files.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!