writing a hex number in the right format
Show older comments
hi guys
i have a problem! I need to send a message to a device like this: "e r 0x02". The hex number is just an example. It could be any number. I normally fetch the number from a GUI interface in a decimal format. In my example the decimal number will be 2.
So somehow i need to convert the decimal number into a hex format which my device can understand. This is what i have tried:
UnitType = dec2hex(str2double(get(handles.Para1,'String')))
str = sprintf('e r 0x%x',UnitType)
This is what i get:
UnitType =
2
str =
e r 0x32
But it should be 'e r 0x02' what is happening?
thx.
Accepted Answer
More Answers (1)
Jan
on 29 Feb 2012
You convert the number to hex twice.
UnitType = str2double(get(handles.Para1,'String'));
str = sprintf('e r 0x%2x', UnitType)
1 Comment
Walter Roberson
on 29 Feb 2012
I would use
str = sprintf('e r 0x%02x', UnitType)
otherwise single-digit numbers would have a space instead of a 0.
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!