Last active
December 18, 2015 07:48
-
-
Save alepez/5748800 to your computer and use it in GitHub Desktop.
parse hex string to bytes. values can be separated by spaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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