简体   繁体   English

MATLAB:将uint32(4字节)值转换为相应的IEEE单精度浮点形式

[英]MATLAB: Converting a uint32 (4-byte) value to the corresponding IEEE single-precision floating-point form

In MATLAB (r2009b) I have a uint32 variable containing the value 2147484101. 在MATLAB(r2009b)中,我有一个包含值2147484101的uint32变量。

This number (its 4-bytes) has been extracted from a digital machine-vision camera in a grabbing process. 这个数字(它的4个字节)是在抓取过程中从数字机器视觉相机中提取的。 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). 根据我的理解,它具有相机快门速度的单精度形式(应接近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? 如何使用MATLAB中提供的内容将此32位数转换为IEEE单精度浮点表示?

With mentioned value in variable n , I have tried using a combination of nn=dec2hex(n,16) and then hex2num(nn) . 对于变量n中提到的值,我尝试使用nn = dec2hex(n,16)hex2num(nn)的组合 But it seems that hex2num expects the hexadecimal coding to be double-precision and not single as it is here. 但似乎hex2num期望十六进制编码是双精度的而不是单一的,因为它在这里。 Atleast I am getting weird numbers with this method. 至少我用这种方法得到了奇怪的数字。

Any ideas? 有任何想法吗?

Edit: Tried @Matt's answer below: 编辑:尝试@Matt的回答如下:

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 . 我在http://www.h-schmidt.net/FloatApplet/IEEE754.html上尝试了IEEE 754转换器(JAVA applet)。

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. 将这些字节输入applet(十六进制)给出了与MATLAB相同的数字。

I think what you're saying is that the underlying bits represent a floating point number, but that you've got it stored as a uint32. 我想你所说的是底层位表示一个浮点数,但是你已将它存储为uint32。

If that's that case, you can cast it (ie reinterpret the bits) as a single precision float using the typecast() function. 如果是这种情况,您可以使用typecast()函数将其转换为(即重新解释位)作为单个精度浮点数。

b = typecast(a, 'single')

where a is your variable. 其中a是你的变量。

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

Edited: not the cast function, the typecast function... My apologies! 编辑:不是演员功能,类型转换功能......我道歉!

You could do the cast when you read the data in with fread(). 使用fread()读取数据时,可以执行转换。

Have a look for the precision argument, you could read it as the int32 number and store it as a single by doing 查看精度参数,您可以将其读作int32数字并将其作为单个数据存储

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM