matlab中32位十六进制到32位浮点(IEEE 754)的转换
如何根据 IEEE 754 将 32 位十六进制值更改为浮点值?
编辑:
...
data = fread(fid,1,'float32');
disp(data);
...
我得到这个答案:
4.2950e+009 1.6274e+009 ...
但是如何获得 32 位浮点 (IEEE 754) 数字?
How can I change the 32 bit hex-value to a floating point value according to the IEEE 754?
EDIT:
...
data = fread(fid,1,'float32');
disp(data);
...
I get this answer:
4.2950e+009
1.6274e+009
...
But how do I get 32 bit floating point (IEEE 754) numbers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的评论之一,您的十六进制值似乎作为字符串存储在文件中。您首先要从文件中以 8 个字符为一组读取这些字符。根据文件的具体格式(例如,每组 8 个字符各占一行,或者它们之间用逗号分隔等),您可以使用诸如 FSCANF 或 TEXTSCAN 来执行此操作。例如,如果您的数据文件如下所示:
那么您可以将数据读入字符数组,如下所示:
现在您需要将这些 32 位十六进制字符串转换为单精度表示形式。到目前为止最简单的方法是使用函数 HEX2DEC 转换字符串到整数(存储为双精度值),使用函数 UINT32,然后使用函数 类型转换。将其应用于上面的示例数据会得到以下结果:
您可以使用 这个在线十六进制到浮点转换器。
如果有人感兴趣,您可以使用函数 HEX2DEC 首先将字符串转换为整数表示形式,然后使用函数 BITGET 提取并处理符号、指数和分数的位单精度数。例如:
Based on one of your comments it appears that your hexadecimal values are stored as strings of characters in a file. You first want to read these characters from the file in groups of 8. Depending on the specific format of your file (e.g. each set of 8 characters is on its own line, or they're separated by commas, etc.), you could use functions like FSCANF or TEXTSCAN to do this. For example, if your data file looks like this:
Then you can read the data into a character array like so:
Now you need to convert these 32-bit hexadecimal strings to single-precision representations. The easiest way by far is to use the function HEX2DEC to convert the strings to integers (stored as double-precision values), convert them to unsigned 32-bit integers using the function UINT32, then cast the 32-bit integers to single-precision representations using the function TYPECAST. Applying this to the sample data I have above gives these results:
You can confirm that these results are correct using this online hexadecimal-to-floating-point converter.
In case anyone is interested, you can do the above type conversion yourself using the function HEX2DEC to first convert the string to an integer representation, then the function BITGET to extract and process the bits for the sign, exponent, and fraction of the single-precision number. For example: