4

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

This number (its 4-bytes) has been extracted from a digital machine-vision camera in a grabbing process. According to what I understand it holds the single-precision form of the shutter-speed of the camera (should be close to 1/260s = 3.8ms).

How do I convert this 32-bit number to its IEEE single-precision floating-point representation - using what's available in MATLAB?

With mentioned value in variable n, I have tried using a combination of nn=dec2hex(n,16) and then hex2num(nn). But it seems that hex2num expects the hexadecimal coding to be double-precision and not single as it is here. Atleast I am getting weird numbers with this method.

Any ideas?

Edit: Tried @Matt's answer below:

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

Which gives:

ans =

  -6.3478820e-043

ans =

  -2.0640313e+003

I tried the IEEE 754 converter (JAVA applet) at 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

2 回答 2

8

我认为您的意思是底层位表示一个浮点数,但是您已将其存储为 uint32。

如果是这种情况,您可以使用 typecast() 函数将其转换为单精度浮点数(即重新解释位)。

b = typecast(a, 'single')

其中 a 是您的变量。

请参阅: http: //www.mathworks.com/help/techdoc/ref/typecast.html

编辑:不是 cast 函数,typecast 函数......我很抱歉!

于 2011-02-15T10:16:16.247 回答
2

当您使用 fread() 读取数据时,您可以进行强制转换。

查看精度参数,您可以将其读取为 int32 数字并将其存储为单个数字

shut_speed=fread(fid,1,'int32=>single');
于 2011-02-15T11:30:45.747 回答