C language Converts a string to a corresponding number (decimal, hexadecimal) "Go"

Source: Internet
Author: User

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.