MATLAB: Hex to float like python struct.unpack()

hex2dechex2numhextodecpythonswapbytestypecast

Hi
I'm trying to convert a hex string to a float in the same manner python function struct.unpack(), https://docs.python.org/3/library/struct.html , which is using the IEEE754 binary32 0r 64 depending on foat or double.
As an example I have
hex = 'C956F53D'
When i run it in a struct.unpack() i get the value:
data = 0.1198
And this is correct. However, when i try the following matlab function i get something totally different.
>> hex2dec(hex)
ans =
3.3779e+09
>> typecast(uint32(hex2dec(hex)),'single')
ans =
single
-8.8047e+05
Does anybody know the difference or what I'm doing wrong?
Thanks!

Best Answer

This is the correct way
hex = 'C956F53D';
x = hex2dec(reshape(hex, 2, []).');
dec = typecast(uint8(x), 'single')