c - scanning a 128 bit input represented in 32 hex values -
i need scan 32 hex number command line , populated uint8_t [16] array, tried scanning string , convert hex hassle since cant find function that, whats best way this?
uint8_t get_int(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return 10 + c - 'a'; if (c >= 'a' && c <= 'f') return 10 + c - 'a'; return -1; } int main(void) { char buff[33]; size_t size; size_t i; uint8_t *output; fgets(buff, sizeof buff, stdin); size = strlen(buff) / 2; output = malloc(size); (i= 0; < size; ++i) output[i] = get_int(buff[2*i]) * 16 + get_int(buff[2*i+1]); (i= 0; < size; ++i) printf("%u ", output[i]); return 0; }
Comments
Post a Comment