将Hex转换为单精度

Pho*_*non 4 floating-point precision matlab hex ieee-754

我正在努力将32位十六进制表达式转换为Matlab中的单精度数.

num2hex功能适用于两者.例如,

>> b = 0.4

b =

   0.400000000000000

>> class(b)

ans =

double

>> num2hex(b)

ans =

3fd999999999999a

>> num2hex(single(b))

ans =

3ecccccd

但是,这不起作用.该hex2num函数将十六进制表达式转换为双精度表达式.所以,

>> b = 0.4

b =

   0.400000000000000

>> num2hex(single(b))

ans =

3ecccccd

>> hex2num(ans)

ans =

    3.433227902860381e-006

Matlab只需填充零,使其成为64位十六进制.有没有办法执行此转换?

小智 10

我发现有一种方法可以在MATLAB中使用内置函数

%#Let's convert 0x40100000 to Single Precision Float (should equal 2.25)

tempHex = '40100000';

tempVal = uint32(hex2dec(tempHex));

tempFloat = typecast(tempVal,'single')
%#result is 2.25
Run Code Online (Sandbox Code Playgroud)


Pau*_*l R 5

它似乎不可能与内置hex2num,但幸运的是你可以在这里获得hex2num(hexsingle2num)的单精度版本:http://www.mathworks.com/matlabcentral/fileexchange/6927-hexsingle2num