How do I convert from HEX to DEC numbers larger than 2^52?

13 vues (au cours des 30 derniers jours)
The MATLAB function "hex2dec" has a input limit of 2^52, how do you convert numbers larger than 2^52 from HEX to DEC in MATLAB?

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 21 Sep 2023
Modifié(e) : MathWorks Support Team le 22 Nov 2023
If the input is greater than 2^52 but smaller than 2^64, consider one of the two options described in the documentation below::
https://www.mathworks.com/help/matlab/ref/hex2dec.html#f72-104326_sep_mw_8a537ff9-c24e-4f07-8539-a5530fa994f8
If the input is greater than or equal to 2^64, you will need to use the Symbolic Toolbox. Please refer to the following example in the documentation for the full workflow:Convert Hexadecimal and Binary Values to Symbolic Decimal Numbers
Below is a simple example code for the workflow:
>> H='0xffffffffffffffffffffffff';
>> D = str2sym(H)
 
D =
 
79228162514264337593543950335
  1 commentaire
Walter Roberson
Walter Roberson le 11 Août 2023
No, you only need symbolic toolbox if the numbers are > 2^64-1

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 11 Août 2023
If the number is larger than 2^52 then if it is an integer it needs to be either int64 or uint64.
H = '400921fb54442d18';
D = sscanf(H, '%lx')
D = uint64 4614256656552045848
dec2hex(D)
ans = '400921FB54442D18'
num2hex(pi)
ans = '400921fb54442d18'
Dd = typecast(D, 'double')
Dd = 3.1416
Dd - pi
ans = 0
We see we bit-for-bit reconstruction
If the value is intended to be int64 instead of uint64, then use the %lx format to get uint64 and then typecast() that to int64

Community Treasure Hunt

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

Start Hunting!

Translated by