Converting HEX to uint8

36 visualizaciones (últimos 30 días)
David
David el 5 de Feb. de 2014
Comentada: David el 26 de Feb. de 2014
Hello I want to convert a very long HEX-String into the uint 8 format because i want so send the uint8 signal through an RS-232 device.
The first step i did, was converting the HEX-String in a decimal number with the hex2dec('HEX-String'. After that i used the uint8 ('previousresult').
My Problem is, that the HEX-String is too long and results in a decimal number of about 1*10^93 this leads to an data-overload.
Can anyone help me?
Thanks a lot!
Greetings
  1 comentario
Patrik Ek
Patrik Ek el 5 de Feb. de 2014
You may want to split up the string into smaller parts. the largest uint8 allowed is 255 (which you probably knew). What you want to have then is something on a format,
original = 'aabbccdd';
wanted = ['aa';'bb';'cc';'dd'];

Iniciar sesión para comentar.

Respuesta aceptada

Patrik Ek
Patrik Ek el 5 de Feb. de 2014
Editada: Patrik Ek el 5 de Feb. de 2014
Try to reshape it first. You of course only want to send in 2hex at a time since the largest unit8 is 255. Try this,
hexstring = 'aabbccddee';
reshapedString = reshape(hexstring,2,5);
hexMtx = reshapedString.';
disp(hexMtx);
decMtx = hex2dec(hexMtx);
disp(decMtx);
intVector = uint8(decMtx);
disp(intVector);
This will give you an uint8 column vector. If you want it as a string format (the uint8 vector) then do,
intString = num2str(intVector');
intString = strrep(intString,' ','');
Good luck and comment if questions.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by