hex2dec broken in R2020a?

39 Ansichten (letzte 30 Tage)
Jorge
Jorge am 12 Mär. 2021
Bearbeitet: Jan am 12 Mär. 2021
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 Kommentar
Jan
Jan am 12 Mär. 2021
Bearbeitet: Jan am 12 Mär. 2021
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.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 12 Mär. 2021
Bearbeitet: Jan am 12 Mär. 2021
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 am 12 Mär. 2021
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

Kategorien

Mehr zu Digital Input and Output finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by