필터 지우기
필터 지우기

Can someone help me how to add 2 hexadecimal numbers?

조회 수: 4 (최근 30일)
Younghun Kim
Younghun Kim 2020년 6월 13일
편집: Walter Roberson 2020년 6월 13일
I have ha and a as string, and I want to add 2 hexadecimal numbers.
ha = '6a09e667f3bcc908'
fn_bin2hex(a) = '73a54f399fa4b1b2'
Can someone help me how to add 2 hexadecimal numbers?
I tryed hex2dec and add 2 number,, but it doesn't work because these numbers are too big.
Is there a correct way to do this?
  댓글 수: 2
KSSV
KSSV 2020년 6월 13일
How about hex2num?
hab = num2hex(hex2num(ha)+hex2num(hb)) ;
Walter Roberson
Walter Roberson 2020년 6월 13일
hex2num() is limited to double, and so cannot be used with values greater than 0x20000000000000

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 13일
If you have symbolic toolbox, then define a function like this
hexsum = @(a, b) dec2hex(sum(str2sym({['0X' a], ['0X' b]})));
Example
ha = '6a09e667f3bcc908';
hb = '73a54f399fa4b1b2';
hc = hexsum(ha, hb);
Result
>> hc
hc =
'DDAF35A193617ABA'
  댓글 수: 4
Younghun Kim
Younghun Kim 2020년 6월 13일
I'm sorry to say this, but there's a problem.
I did addition another 2 num.
'3c6ef372fe94f82b' + 'd67806db8b148677'
ans =
'112E6FA4E89A97EA2'
I think it has to be '12e6fa4e89a97ea2'
Why is one bit stuck in front? I don't know why...... :(
Walter Roberson
Walter Roberson 2020년 6월 13일
편집: Walter Roberson 2020년 6월 13일
'112E6FA4E89A97EA2' is correct, and your expectation of '12e6fa4e89a97ea2' is incorrect.
Look at the top nibble: you are adding 3 + d, which is 3 + 13, and that is going to overflow. Therefore the result must have one more digit than the inputs, 17 output digits when the inputs are 16 digits each.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by