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

发布于 2024-10-17 08:17:53 字数 1099 浏览 2 评论 0原文

在 MATLAB (r2009b) 中,我有一个包含值 2147484101 的 uint32 变量。

该数字(4 个字节)是在抓取过程中从数字机器视觉相机中提取的。据我了解,它保存了相机快门速度的单精度形式(应接近 1/260s = 3.8ms)。

如何使用 MATLAB 中提供的功能将此 32 位数字转换为其 IEEE 单精度浮点表示形式?

对于变量n中提到的值,我尝试使用nn=dec2hex(n,16)hex2num(nn)的组合。但似乎 hex2num 期望十六进制编码是双精度的,而不是像这里一样是单精度的。至少我用这种方法得到了奇怪的数字。

有什么想法吗?

编辑:尝试了下面@Matt的答案:

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

给出了:

ans =

  -6.3478820e-043

ans =

  -2.0640313e+003

我在http://www.h-schmidt.net/FloatApplet/IEEE754.html

使用:

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

给出

ans =

   c5   01   00   80

ans =

   80   00   01   c5

将这些字节输入小程序(十六进制)会得到与 MATLAB 相同的数字。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

森林很绿却致人迷途 2024-10-24 08:17:53

我认为您所说的是底层位代表浮点数,但您已将其存储为 uint32。

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

b = typecast(a, 'single')

其中 a 是你的变量。

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

编辑:不是强制转换函数,而是强制转换函数...我很抱歉!

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.

If that's that case, you can cast it (i.e. reinterpret the bits) as a single 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 the cast function, the typecast function... My apologies!

尐籹人 2024-10-24 08:17:53

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

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

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

You could do the cast when you read the data in with fread().

Have a look for the precision argument, you could read it as the int32 number and store it as a single by doing

shut_speed=fread(fid,1,'int32=>single');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文