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

조회 수: 13 (최근 30일)
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?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2023년 9월 21일
편집: MathWorks Support Team 2023년 11월 22일
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
Walter Roberson
Walter Roberson 2023년 8월 11일
No, you only need symbolic toolbox if the numbers are > 2^64-1

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2023년 8월 11일
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