Skip to content

Instantly share code, notes, and snippets.

@clzola
Created April 20, 2016 11:05
Show Gist options
  • Save clzola/b7f1d9dfacab7a253afce7ca3d5aeb23 to your computer and use it in GitHub Desktop.
Save clzola/b7f1d9dfacab7a253afce7ca3d5aeb23 to your computer and use it in GitHub Desktop.
unsigned char* hex2bin(const char* hexstr, size_t* size)
{
size_t hexstrLen = strlen(hexstr);
size_t bytesLen = hexstrLen / 2;
unsigned char* bytes = (unsigned char*) malloc(bytesLen);
int count = 0;
const char* pos = hexstr;
for(count = 0; count < bytesLen; count++) {
sscanf(pos, "%2hhx", &bytes[count]);
pos += 2;
}
if( size != NULL )
*size = bytesLen;
return bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment