Adding Hexadecimal numbers?

64 ビュー (過去 30 日間)
Jed Louise Ledesma
Jed Louise Ledesma 2019 年 10 月 17 日
回答済み: Walter Roberson 2019 年 10 月 17 日
Can someone help me how to add 2 hexadecimal numbers? I would appreciate the help and sentiment.
I tried using: c=dec2hex(hex2dec(a)+hex2dec(b)));
But it didn't work. Is there a correct way to do this?

採用された回答

Robert U
Robert U 2019 年 10 月 17 日
Hi Jed Louise Lesma,
Despite the extra bracket in your code (I removed it), the method you tried should work:
a = '3afb';
b = '2839';
c=dec2hex(hex2dec(a)+hex2dec(b));
Kind regards,
Robert
  3 件のコメント
Robert U
Robert U 2019 年 10 月 17 日
The "hex"-values are supposed to be given as char. Thus the str2double()-command should be deleted.
Kind regards,
Robert
Jed Louise Ledesma
Jed Louise Ledesma 2019 年 10 月 17 日
I appreciate what you did.
Thank you!

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

その他の回答 (2 件)

Steven Lord
Steven Lord 2019 年 10 月 17 日
If you're using release R2019b or later, you can enter hexadecimal numbers directly.
>> 0x3afb + 0x2839
ans =
uint16
25396
>> hex2dec('3afb')+hex2dec('2839')
ans =
25396

Walter Roberson
Walter Roberson 2019 年 10 月 17 日
If there is a possibility that the user might enter hex values with more than 53 significant bits then you should use sscanf with %lx format instead of hex2dec.

カテゴリ

Help Center および File ExchangeDigital Input and Output についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by