Adding Hexadecimal numbers?
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jed Louise Ledesma
am 17 Okt. 2019
Beantwortet: Walter Roberson
am 17 Okt. 2019
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?
0 Kommentare
Akzeptierte Antwort
Robert U
am 17 Okt. 2019
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 Kommentare
Robert U
am 17 Okt. 2019
The "hex"-values are supposed to be given as char. Thus the str2double()-command should be deleted.
Kind regards,
Robert
Weitere Antworten (2)
Steven Lord
am 17 Okt. 2019
>> 0x3afb + 0x2839
ans =
uint16
25396
>> hex2dec('3afb')+hex2dec('2839')
ans =
25396
0 Kommentare
Walter Roberson
am 17 Okt. 2019
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!