I'm trying to create an ASN.1 BITSTRING
To start I have a long hex string like "abcd..."
which I believe needs to become { 0xab, 0xcd ... }
in the output but not sure where to start (sscanf()
on 2 chars at a time?). Realized this after incorrectly converting to char[]
with 1010101111001101
but this is not the binary output, it is the binary digits shown as a string.
How to convert a hexadecimal string to a binary string in C doesn't seem to work for me (the res array comes out empty).
EDIT1
Based on the comments (and before looking at the excellent answers) I came up with the following. Will let others comment before marking an answer.
char input[] = "abcd";
char output[MAX_LENGTH];
char c[3];
int p = 0;
int b = 0;
while (input[p])
{
strncpy(c, input + p, 2);
c[2] = '\0';
p += 2;
output[ b++ ] = strtol(c, NULL, 16);
}