To convert a number to a 16-digit string, we can call sprintf, but the standard library of C does not provide a function that converts a hexadecimal string number to an integer number. I've written a feature that can be implemented and optimized as much as possible.
(Currently this function only supports 32-bit)/* input hex string, format:0xaa111 or AA111 or abc11/int ahextoi (char* p) {int n = 0;
char* q = p;
/* Reach's Tail/while (*Q) q++;
if (*p = = ' 0 ' && * (P + 1)!= 0)/* skip "0x" or "0X" * * + + 2;
while (*p) {int C;
if (*p >= ' 0 ' && *p <= ' 9 ') c = *p-' 0 ';
else if (*p >= ' a ' && *p <= ' F ') c = *p-' a ' + 0xA;
else if (*p >= ' a ' && *p <= ' f ') c = *p-' a ' + 0xA;
else/* Invalid char */return 0;
n + + C << ((int) (Q-P-1) * 4);
p++;
} return N;
You can test with the following code: int main (int argc, char* argv[]) {int out = 0;
out = Ahextoi ("");
printf ("% #010X/n", out);
out = Ahextoi ("0x123");
printf ("% #010X/n", out); Out = Ahextoi ("123abc");
printf ("% #010X/n", out);
out = Ahextoi ("ABC123");
printf ("% #010X/n", out);
out = Ahextoi ("0xac1b1c");
printf ("% #010X/n", out);
return 0;
}