32位十六进制到32位浮点(IEEE 754)在matlab中的转换 [英] 32 bit hex to 32 bit floating point (IEEE 754) conversion in matlab

查看:6277
本文介绍了32位十六进制到32位浮点(IEEE 754)在matlab中的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将32位十六进制值更改为符合IEEE 754的浮点值?编辑:

  ... 
data = fread(fid,1,'float32');
disp(data);
...

我得到这个答案:

4.2950e + 009
1.6274e + 009
...

但是我怎么得到32位的浮点数IEEE 754)编号?根据您的评论之一,您的十六进制值存储为一个字符的字符串在一个文件。你首先要从文件中以8个组读取这些字符。根据你的文件的具体格式(例如,每个8个字符的集合都在它自己的行上,或者它们用逗号分隔),你可以使用诸如 FSCANF TEXTSCAN 来做到这一点。例如,如果您的数据文件如下所示:

  409BFFFF 
3B3C0000
85E60000
然后你可以像这样读取数据到一个字符数组:

> $ b>

  fid = fopen(fileName,'r'); %#打开文件
data = textscan(fid,'%s'); %#读取数据
charArray = char(data {1}); %#创建一个字符数组
fclose(fid); %#关闭文件

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

 >> values = typecast(uint32(hex2dec(charArray)),'single'); 
>> fprintf('%1.42f \\\
',values); %#显示值
4.87499952316284180000000000000000000000
0.002868652343750000000000000000000000000000
-0.000000000000000000000000000000000021629096

您可以使用这个在线的十六进制到浮点转换器来确认这些结果是否正确






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

 >> a ='409BFFFF'; %#十六进制的示例
>> b = hex2dec(a); %#转换为整数
>> sign = bitget(b,32); %#计算符号
>> exponent = bitget(b,24:31)* 2。^(0:7)。 %'#计算指数
>>分数= bitget(b,1:23)* 2。^( - 23:-1)。 %'#计算分数
>>值=(-1)^符号*(1 +分数)* 2 ^(指数-127); %#计算值
>> fprintf('%1.7f\\\
',value)%#显示值
4.8749995


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?

解决方案

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

这篇关于32位十六进制到32位浮点(IEEE 754)在matlab中的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆