MATLAB: converting a uint32 value (4 bytes) to the corresponding IEEE single precision floating point form

In MATLAB (r2009b), I have a uint32 variable containing the value 2147484101.

This number (4 bytes) was extracted from a digital camera of machine vision during capture. In accordance with what I understand, it takes the form of the accuracy of a single shutter speed of the camera shutter (should be close to 1/260 s = 3.8 ms).

How can I convert this 32-bit number to its IEEE single-point floating-point representation - using what is available in MATLAB?

With the specified value in the variable n, I tried to use the combination nn = dec2hex (n, 16), and then hex2num (nn). But it looks like hex2num expects hexadecimal encoding to have double precision and not be equivalent, as here. At least I get weird numbers with this method.

Any ideas?

Edit: Tried to answer @Matt below:

typecast(uint32(2147484101),'single') %# without swapbytes typecast(swapbytes(uint32(2147484101)),'single') %# with swapbytes 

What gives:

 ans = -6.3478820e-043 ans = -2.0640313e+003 

I tried the IEEE 754 converter (JAVA applet) to http://www.h-schmidt.net/FloatApplet/IEEE754.html .

Using:

 format hex typecast(uint32(2147484101),'uint8') %# without swapbytes typecast(swapbytes(uint32(2147484101)),'uint8') %# with swapbytes 

gives

 ans = c5 01 00 80 ans = 80 00 01 c5 

Entering these bytes into the applet (hexadecimal) gives me the same numbers as MATLAB.

+4
source share
2 answers

I think you are saying that the base bits are a floating point number, but you saved it as uint32.

If in this case you can use it (for example, to re-interpret bits) as one precision float using the typecast () function.

 b = typecast(a, 'single') 

where a is your variable.

See: http://www.mathworks.com/help/techdoc/ref/typecast.html

Edited: not broadcast function, type function ... My apologies!

+8
source

You can throw when you read data with fread ().

Look for the precision argument, you can read it as an int32 number and save it as a single by doing

 shut_speed=fread(fid,1,'int32=>single'); 
+2
source

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


All Articles