Convert hexadecimal to single precision

I am trying to convert a 32-bit hexadecimal expression to a single precision number in Matlab.

The num2hex function num2hex great for both. For instance,

  >> b = 0.4

 b =

    0.400000000000000

 >> class (b)

 ans =

 double

 >> num2hex (b)

 ans =

 3fd999999999999a

 >> num2hex (single (b))

 ans =

 3ecccccd

However, this does not work the other way around. The hex2num function hex2num converts a hexadecimal expression to double. In this way,

  >> b = 0.4

 b =

    0.400000000000000

 >> num2hex (single (b))

 ans =

 3ecccccd

 >> hex2num (ans)

 ans =

     3.433227902860381e-006

Matlab simply strips zeros to make it 64-bit hexadecimal. Is there any way to do this conversion?

+4
source share
2 answers

Apparently this is not possible with the built-in hex2num , but fortunately you can get one version of the precision hex2num ( hexsingle2num ) here: http://www.mathworks.com/matlabcentral/fileexchange/6927-hexsingle2num

+5
source

Where I found to do this using the built-in functions in MATLAB

 %#Let convert 0x40100000 to Single Precision Float (should equal 2.25) tempHex = '40100000'; tempVal = uint32(hex2dec(tempHex)); tempFloat = typecast(tempVal,'single') %#result is 2.25 
+10
source

Source: https://habr.com/ru/post/1369185/


All Articles