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

13 ビュー (過去 30 日間)
MathWorks Support Team
MathWorks Support Team 2014 年 8 月 14 日
編集済み: MathWorks Support Team 2023 年 11 月 22 日
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