matlab中32位十六进制到32位浮点(IEEE 754)的转换

发布于 2024-10-20 20:33:55 字数 219 浏览 2 评论 0原文

如何根据 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 技术交流群。

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

发布评论

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

评论(1

歌枕肩 2024-10-27 20:33:55

根据您的评论之一,您的十六进制值似乎作为字符串存储在文件中。您首先要从文件中以 8 个字符为一组读取这些字符。根据文件的具体格式(例如,每组 8 个字符各占一行,或者它们之间用逗号分隔等),您可以使用诸如 FSCANFTEXTSCAN 来执行此操作。例如,如果您的数据文件如下所示:

409BFFFF
3B3C0000
85E60000

那么您可以将数据读入字符数组,如下所示:

fid = fopen(fileName,'r');  %# Open the file
data = textscan(fid,'%s');  %# Read the data
charArray = char(data{1});  %# Create a character array
fclose(fid);                %# Close the file

现在您需要将这些 32 位十六进制字符串转换为单精度表示形式。到目前为止最简单的方法是使用函数 HEX2DEC 转换字符串到整数(存储为双精度值),使用函数 UINT32,然后使用函数 类型转换。将其应用于上面的示例数据会得到以下结果:

>> values = typecast(uint32(hex2dec(charArray)),'single');
>> fprintf('% 1.42f\n',values);  %# Display the values
 4.874999523162841800000000000000000000000000
 0.002868652343750000000000000000000000000000
-0.000000000000000000000000000000000021629096

您可以使用 这个在线十六进制到浮点转换器


如果有人感兴趣,您可以使用函数 HEX2DEC 首先将字符串转换为整数表示形式,然后使用函数 BITGET 提取并处理符号、指数和分数的位单精度数。例如:

>> a = '409BFFFF';  %# A sample hexadecimal value
>> b = hex2dec(a);  %# Convert to an integer
>> sign = bitget(b,32);                               %# Compute the sign
>> exponent = bitget(b,24:31)*2.^(0:7).';            %'# Compute the exponent
>> fraction = bitget(b,1:23)*2.^(-23:-1).';          %'# Compute the fraction
>> value = (-1)^sign*(1+fraction)*2^(exponent-127);   %# Compute the value
>> fprintf('%1.7f\n',value)                           %# Display the value
4.8749995

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:

409BFFFF
3B3C0000
85E60000

Then you can read the data into a character array like so:

fid = fopen(fileName,'r');  %# Open the file
data = textscan(fid,'%s');  %# Read the data
charArray = char(data{1});  %# Create a character array
fclose(fid);                %# Close the file

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:

>> values = typecast(uint32(hex2dec(charArray)),'single');
>> fprintf('% 1.42f\n',values);  %# Display the values
 4.874999523162841800000000000000000000000000
 0.002868652343750000000000000000000000000000
-0.000000000000000000000000000000000021629096

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:

>> a = '409BFFFF';  %# A sample hexadecimal value
>> b = hex2dec(a);  %# Convert to an integer
>> sign = bitget(b,32);                               %# Compute the sign
>> exponent = bitget(b,24:31)*2.^(0:7).';            %'# Compute the exponent
>> fraction = bitget(b,1:23)*2.^(-23:-1).';          %'# Compute the fraction
>> value = (-1)^sign*(1+fraction)*2^(exponent-127);   %# Compute the value
>> fprintf('%1.7f\n',value)                           %# Display the value
4.8749995
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文