Hello I have a hex cell array and I indexed them.
For example I have 'A7' and when I use hex2dec, it gives me nonsense result which is -7.7452e-121. how can I get exact result? I mean I want to see that hex A7 = decimal 167.
Thank you.

6 件のコメント

Chunru
Chunru 2022 年 6 月 3 日
Can you show your cell array?
Rik
Rik 2022 年 6 月 3 日
Please post code that reproduces this issue. You can run code within the editor to make sure the effect matches what you see in your local copy.
tinkyminky93
tinkyminky93 2022 年 6 月 3 日
My cell array is like
4x1 cell array
{'3A'} {'F1'} {'CF'} {'4C'}
tinkyminky93
tinkyminky93 2022 年 6 月 3 日
I can not post it there because I am reading these values from data log.
Stephen23
Stephen23 2022 年 6 月 3 日
編集済み: Stephen23 2022 年 6 月 3 日
hex2dec('A7') % what you should have used
ans = 167
hex2num('A7') % what you actually used
ans = -7.7452e-121
The unfortunately named NUM2HEX and HEX2NUM convert to/from the IEEE 754 floating point hexadecimal representation, they are completely unsuitable for your task. Just for interest, the complete hexadecimal is:
num2hex(hex2num('A7'))
ans = 'a700000000000000'
Apparently someone thought that it would be a good idea to pad its input with trailing zeros. Ugh.
Chunru
Chunru 2022 年 6 月 3 日
編集済み: Chunru 2022 年 6 月 3 日
hex2num inteprete 'A7' as IEEE double number (64bit) with mantissa and exponents. So padding 0's before 'A7'. The string is in a different order (from last byte to first byte) so it looks like (actually not) padding 0's after 'A7'.

サインインしてコメントする。

 採用された回答

Chunru
Chunru 2022 年 6 月 3 日

0 投票

x ={'3A', 'F1', 'CF', '4C'}
x = 1×4 cell array
{'3A'} {'F1'} {'CF'} {'4C'}
y = cellfun(@hex2dec, x)
y = 1×4
58 241 207 76

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 6 月 3 日

編集済み:

2022 年 6 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by