/* hex_string - input hex string
* write_data - oupt uint8 array
* write_size - size of bytes to write
*
* Function does not detect invalid hex char, usr should make sure to use valid hex value.
*/
bool parse_hex_string(char* hex_string, uint8_t *write_data, uint32_t write_size)
{
if(hex_string[0] == '0' && (hex_string[1] == 'X' || hex_string[1] == 'x'))
{
char byte[3] = {0};
for(uint32_t i = 1; i < write_size + 1; i++)
{
byte[0] = hex_string[i*2];
byte[1] = hex_string[i*2 + 1];
write_data[i - 1] = strtoul(byte, NULL, 16);
}
return true;
}
else
{
printf("Write hex string parse wrong!\n");
return false;
}
}
Hex string is like:
0X1122334455667788AABBCCDDEEFF
====================
Sample input/output
0x112233445566778899AA
Write buffer before swap:
112233445566778899AA0000
Write buffer after swap:
44332211887766550000AA99 <---extra 2 zeros added here!
Write buffer swap false:
112233445566778899AA0000
Write: 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 0x99 0xaa