Can someone help me how to add 2 hexadecimal numbers?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Younghun Kim
el 13 de Jun. de 2020
Editada: Walter Roberson
el 13 de Jun. de 2020
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/315210/image.png)
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 comentarios
Walter Roberson
el 13 de Jun. de 2020
hex2num() is limited to double, and so cannot be used with values greater than 0x20000000000000
Respuesta aceptada
Ameer Hamza
el 13 de Jun. de 2020
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 comentarios
Walter Roberson
el 13 de Jun. de 2020
Editada: Walter Roberson
el 13 de Jun. de 2020
'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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!