Take the 2-minute tour ×
Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

I have a string which I send to the arduino using serial monitor. Something like that:

my specific string 0x0F 0x2C 0x98 0xBC

How can I parse this string in the arduino to get an array of hex values:

0x0F 0x2C 0x98 0xBD

? There are two prerequisites:

  1. All hexadecimal starts with #
  2. The size of hexadecimal is 2. No 0x111 or '0xbbb'
share|improve this question

1 Answer 1

You can try reading each character and convert it to hexadecimal representation. I suppose you wanna 1 byte each value in hexadecimal representation, so, let's start.

For example the string "0x25", ignore the 0x and read the '2' and save it into a char variable. Same for '5';

char highBits = '2' - '0'; // convert to decimal representation
char lowBits  = '5' - '0'; 

byte = (highBits << 4 | lowBits);

Now, save it inside you array. Make sure you check if the value is one of the possible letters in hexadecimal and convert it properly (A=10 and so on). Just include an if statement if it is a letter and convert it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.