Converting HEX to uint8

36 次查看(过去 30 天)
David
David 2014-2-5
评论: David 2014-2-26
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 个评论
Patrik Ek
Patrik Ek 2014-2-5
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'];

请先登录,再进行评论。

采纳的回答

Patrik Ek
Patrik Ek 2014-2-5
编辑:Patrik Ek 2014-2-5
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by