Transferred from: http://wawlian.iteye.com/blog/1315133
Issue 1: A string representation of a decimal number is converted to the corresponding integer. Example: the "1234"converted to an integer 1234. C Code Collection Code/*converts the string s to the corresponding integer*/ intAtoiChars[]) { inti; intn =0; for(i =0; S[i] >='0'&& S[i] <='9'; ++i) {n=Ten* n + (S[i]-'0'); } returnN; Problem 2: Converts the string representation of a hexadecimal number to the corresponding integer. The string form of the so-called hexadecimal number refers to a string that contains only'0'-'9'Or'a'-'Z'Or'A'-'Z', the leading "0x" or "0X" can appear. To solve this problem, you also need a tool function that converts uppercase letters to lowercase letters: C Code Collection Code/*Convert uppercase letters to lowercase*/ intToLowerintc) {if(c >='A'&& C <='Z') { returnC +'a'-'A'; } Else { returnC; The following is a conversion function: C Code Collection Code//converts a hexadecimal string into an integerintHtoi (Chars[]) { inti; intn =0; if(s[0] =='0'&& (s[1]=='x'|| s[1]=='X') ) {i=2; } Else{i=0; } for(; (S[i] >='0'&& S[i] <='9') || (S[i] >='a'&& S[i] <='Z') || (S[i] >='A'&& S[i] <='Z');++i) {if(ToLower (s[i]) >'9') {n= -* n + (Ten+ ToLower (S[i])-'a'); } Else{n= -* n + (ToLower (s[i))-'0'); } } returnN; }
C language Converts a string to a corresponding number (decimal, hexadecimal) "Go"