将十六进制转换为单精度
我正在努力在 Matlab 中将 32 位十六进制表达式转换为单精度数字。
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 位十六进制。有没有办法执行这种转换?
I'm struggling with converting a 32-bit hex expression into a single precision number in Matlab.
The num2hex
function works fine for both. For example,
>> b = 0.4 b = 0.400000000000000 >> class(b) ans = double >> num2hex(b) ans = 3fd999999999999a >> num2hex(single(b)) ans = 3ecccccd
However, this does not work the other way around. The hex2num
function only converts hexadecimal expression into doubles. So,
>> b = 0.4 b = 0.400000000000000 >> num2hex(single(b)) ans = 3ecccccd >> hex2num(ans) ans = 3.433227902860381e-006
Matlab simply pads zeros to make it a 64-bit hex. Is there a way to perform this conversion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现有一种方法可以使用 MATLAB 中的内置函数来完成此操作
There's a way I found to do this usign built in functions in MATLAB
使用内置的
hex2num
似乎不可能,但幸运的是你可以获得单精度版本的hex2num
(hexsingle2num
)此处:http://www.mathworks.com/matlabcentral/fileexchange/6927-hexsingle2numIt doesn't seem to be possible with the built-in
hex2num
, but fortunately you can get a single precision version ofhex2num
(hexsingle2num
) here: http://www.mathworks.com/matlabcentral/fileexchange/6927-hexsingle2num