hex2dec broken in R2020a?

27 次查看(过去 30 天)
Jorge
Jorge 2021-3-12
编辑: Jan 2021-3-12
Hi! We used to be able to do the following, at least up to R2019a:
>> hex2dec('ad55cb92eef08aea93f27c40457f8835')
ans =
2.304019174741754e+38
Now we switched to R2020a, and I get:
>> hex2dec('ad55cb92eef08aea93f27c40457f8835')
Error using hex2dec
Hexadecimal text has too many digits for specified or implied type suffix.
...how can I achieve the old behavior?
Thanks in advance for any help!
P.S. In our particular application, we don't care about the precision of the conversion, as the hex input is actually a hash generated by DataHash(string).
  1 个评论
Jan
Jan 2021-3-12
编辑:Jan 2021-3-12
Do you have a good reason to convert the hash to a decimal value? I cannot imagine how this could be useful. The has contains 128 bits, but a double value only 53.

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2021-3-12
编辑:Jan 2021-3-12
hex2dec is much slower than sscanf(s, '%x'):
value = pow2([96, 64, 32, 0]) * sscanf(str, '%8x')
During the conversion to a double you reduce the precision from 128 to 53 bits. So it is enough to convert the first half:
value = pow2([96, 64]) * sscanf(str(1:16), '%8x')

Stephen23
Stephen23 2021-3-12
format long
str = 'ad55cb92eef08aea93f27c40457f8835';
val = hex2dec(str(01:16))*pow2(64) + hex2dec(str(17:32))
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
val =
2.304019174741754e+38

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by