Convert Hexadecimal String to Data
Posted
by AriX
on Stack Overflow
See other posts from Stack Overflow
or by AriX
Published on 2010-05-18T12:20:31Z
Indexed on
2010/05/18
23:20 UTC
Read the original article
Hit count: 311
Hi Stack Overflow,
I have found a whole lot of different solutions to this problem, but not all of them work, and a lot of them seem somewhat hacky and inefficient. Basically I have a string of hexadecimal data (i.e. "55 AA 41 2A 00 94 55 AA BB BB 00 FF") which I would like to convert to raw data. What is the best way to do this?
UPDATE: Vicky's solution worked great for me, but I changed it to work with hexadecimal strings that don't have spaces in between and changed the style a bit.
int i = 0;
char *hexString = "55AA412A009455AABBBB00FF"
char *hexPtr = hexString;
unsigned int *result = calloc(strlen(hexString)/2 + 1, sizeof *result);
while (sscanf(hexPtr, "%02x", &result[i++])) {
hexPtr += 2;
if (hexPtr >= hexString + strlen(hexString)) break;
}
return result;
© Stack Overflow or respective owner