How to Convert data from HEX codes to Numeric

조회 수: 6 (최근 30일)
Harsimran Singh
Harsimran Singh 2023년 11월 9일
댓글: Walter Roberson 2023년 11월 9일
Hello, how can I convert data given in HEX codes to decimal.
  1. Read complete file as it is
  2. Output: Add a second column with values in Numeric.

답변 (2개)

Voss
Voss 2023년 11월 9일
format long g
T = readtable('hex codes example.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
T = 19×1 table
HexCodes ___________________________ {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'00 FC' } {'FA 80 3E 11 01 04 00 00'} {'00 81 FF FF' } {'FA 80 3E 11 01 04 00 00'}
T.DecCodes = hex2dec(strrep(T.HexCodes,' ',''))
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
T = 19×2 table
HexCodes DecCodes ___________________________ ____________________ {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'00 FC' } 252 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19 {'00 81 FF FF' } 8519679 {'FA 80 3E 11 01 04 00 00'} 1.80504955492534e+19
writetable(T,'hex and dec codes example.xlsx')

Steven Lord
Steven Lord 2023년 11월 9일
Do you mean you have the hex representation of a double precision number and want to convert it into the corresponding double? See the num2hex and hex2num functions.
Or are you trying to convert integer values? In that case see the dec2hex and hex2dec functions.
  댓글 수: 1
Walter Roberson
Walter Roberson 2023년 11월 9일
hex2num() can only handle double precision . You need other approaches for single precision.
But they are unlikely to be single precision or double precision.
format long g
H2=[0xfa 0x80 0x3e 0x11 01 04 00 00]
H2 = 1×8
250 128 62 17 1 4 0 0
typecast(H2, 'double')
ans =
2.1751886233388e-311
swapbytes(ans)
ans =
-1.17933560861813e+282
typecast(H2, 'single')
ans = 1×2
1.0e+00 * 1.50281e-28 1.436331e-42
swapbytes(ans)
ans = 1×2
1.0e+00 * -3.329364e+35 2.424457e-38

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

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by