【问题标题】:Convert Hex to single precision将十六进制转换为单精度
【发布时间】:2011-08-26 15:54:52
【问题描述】:

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

num2hex 函数对两者都适用。例如,

>> b = 0.4 b = 0.400000000000000 >> 类(b) 答案= 双倍的 >> num2hex(b) 答案= 3fd999999999999a >> num2hex(单(b)) 答案= 3eccccd

但是,反过来就不行了。 hex2num 函数only 将十六进制表达式转换为双精度。所以,

>> b = 0.4 b = 0.400000000000000 >> num2hex(单(b)) 答案= 3eccccd >> hex2num(ans) 答案= 3.433227902860381e-006

Matlab 只是简单地填充零以使其成为 64 位十六进制。有没有办法执行这种转换?

【问题讨论】:

标签: matlab floating-point hex precision ieee-754


【解决方案1】:

我发现有一种方法可以在 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

【讨论】:

  • 这是最好的答案,因为它提供了所需的确切功能
  • 如果hex2num 不可用,typecast(uint64(hex2dec('400921fb54442d18')),'double') 有效
【解决方案2】:

使用内置的hex2num 似乎不可能,但幸运的是,您可以在此处获得hex2num (hexsingle2num) 的单精度版本:http://www.mathworks.com/matlabcentral/fileexchange/6927-hexsingle2num

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-05
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多