So I'm assuming that an ackMessage
must start with 'F'
and end with '\r'
. And I'm even assuming that, since you work on a micro controller, anything that does more than required (eg. library functions) should be avoided. Given that you just have to iterate over the buffer:
uint8_t charToHex(unsigned char value)
{
if (value >= '0' && value <= '9') return value - '0';
else if (value >= 'A' && value <= 'F') return value - 'A' + 10;
else if (value >= 'a' && value <= 'f') return value - 'a' + 10;
assert(false);
}
#define BUFFER_SIZE (20)
int main(void)
{
char ackBuffer[BUFFER_SIZE] = "F12345890AB\r";
uint8_t result[(BUFFER_SIZE-2)/2]; // at most 9 bytes since it's 20 - 2 / 2
char* ptr = ackBuffer;
assert(*ptr == 'F');
++ptr;
int count = 0;
while(*ptr != '\r')
{
uint8_t value = charToHex(*ptr) << 4 | charToHex(*(ptr+1));
result[count++] = value;
ptr += 2;
}
int i;
for (i = 0; i < count; ++i)
printf("%x\n", result[i]);
return 0;
}
Mind that you must have a charToHex
consistent with the used encoding and probably you could need some more sanity checks around the code.