I need to convert a string containing hexadecimal values as characters to an array of bytes. Although this answer has already been answered here , I get the following error:
warning: ISO C90 does not support the 'hh' gnu_scanf length modifier [-Wformat]
Since I don't like warnings, and omitting hh just creates another warning
warning: format '%x' expects argument of type 'unsigned int *', but argument 3 has type 'unsigned char *' [-Wformat]
My question is: how to do it right? To complete, I will send an example code again:
#include <stdio.h> int main(int argc, char **argv) { const char hexstring[] = "deadbeef10203040b00b1e50", *pos = hexstring; unsigned char val[12]; size_t count = 0; /* WARNING: no sanitization or error-checking whatsoever */ for(count = 0; count < sizeof(val)/sizeof(val[0]); count++) { sscanf(pos, "%2hhx", &val[count]); pos += 2 * sizeof(char); } printf("0x"); for(count = 0; count < sizeof(val)/sizeof(val[0]); count++) printf("%02x", val[count]); printf("\n"); return(0); }
c arrays type-conversion
Alex Aug 16 '13 at 7:14 2013-08-16 07:14
source share