This article describes how C + + implements a hexadecimal string conversion to a decimal integer. Share to everyone for your reference. The implementation method is as follows:
* * Converts a string of hexadecimal digits (including an optional prefix of 0x or 0X) to an integral value equivalent to it/* #include <stdio.h> #include <math.h
>/* Converts characters in hexadecimal to the corresponding integer */int hexchtoi (char hexch) {char phexch[] = "ABCDEF";
Char qhexch[] = "abcdef";
int i; for (i=0;i<6;i++) {if (Hexch = = Phexch[i]) | |
(Hexch = = Qhexch[i])
Break
printf ("i=%d", I);
if (I >= 6) {return 0;/* non-hexadecimal character/} return 10+i;
int Htoi (char s[]) {int n=0;//* has n-bit/int valu=1;/* valid/int i=0,j;
int answer=0; /* Validity Check/if ((s[0] = = ' 0 ') && ((s[1] = = ' x ') | | (s[1] = = ' X '))
{i = 2;
while ((s[i)!= ' \ n ')) {if (S[i] < ' 0 ') && (S[i] > ' 9 ')) {if (Hexchtoi (s[i)) = = 0) {valu=0;
Break
}} n++;
i++;
} if (Valu!= 0) {for (j=0;j<n;j++) {answer = = (int) POW (16,j) * Hexchtoi (s[i-j-1)));
} else Answer =-1;
return answer;
Main () {char *n[] = {"0x7ff0", "0x2341"};
printf ("%s is%d\n", N[0],htoi (N[0]));
printf ("%s is%d\n", n[0],123); }
I hope this article will help you with your C + + programming.