Skip to content

Instantly share code, notes, and snippets.

@alepez
Last active December 18, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alepez/5748800 to your computer and use it in GitHub Desktop.
Save alepez/5748800 to your computer and use it in GitHub Desktop.
parse hex string to bytes. values can be separated by spaces
void parseData(const char* dataStr, unsigned char* data, int* length) {
::memset(data, 0, sizeof(data));
const char* ds = dataStr;
unsigned char* d = data;
char byteStr[3] = { 0 };
while (true) {
if (ds[0] == ' ') {
++ds;
}
if (ds[0] == 0 || ds[1] == 0) {
break;
}
byteStr[0] = ds[0];
byteStr[1] = ds[1];
char* endPtr = 0;
int num = strtoul(byteStr, &endPtr, 16);
printf("%c %c => %i\n", ds[0], ds[1], num);
*d = num;
++(*length);
ds += 2;
++d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment