I'm given a string hex_txt containing a 4 digit hex number in the way outlined in the code, split up in two array entries. I need to convert it to decimal. The following is the way I'm doing it.
unsigned char hex_txt[] = "\xAB\xCD";
unsigned char hex_num[5];
unsigned int dec_num;
sprintf(hex_num, "%.2x%.2x", (int)hex_txt[0], (int)hex_txt[1]);
printf("%s\n", hex_num);
sscanf(hex_num, "%x", &dec_num);
printf("%d\n", dec_num);
Is there a faster, or more efficient way of doing this? This is my current ad hoc solution, but I'd like to know if there's a proper way to do it.