Integer conversion to string Inttoarray and string to integer Arraytoint implementation

Source: Internet
Author: User

7. Implementation of string itoa for integer loading

1 Char*inttoarray (intNumberChar*str)2 {3     if(str = =NULL)4     {5         returnNULL;6     }7     Char*ret =str;8     Char*left =str;9     Chartemp =' /';Ten     //handling of negative numbers One     if(Number <0) A     { -Number =-Number ; -*ret++ ='-'; theleft++; -     } -     //the number of different bits separated by the 10 method of finding the remainder -      while(Number >0) +     { -*ret++ = number%Ten+'0';//Convert a number to a Ascall code +Number = number/Ten; A     } at*ret =' /'; -ret--; -      -     /*Reverse*/ -      while(Ret >Left ) -     { intemp = *Left ; -*left++ = *ret; to*ret--=temp; +     } -     returnstr; the}

Convert String to Integer arraytoint

consider whether the string is negative, contains letters, and if the converted number overflows, and so on, the processing -123asd455 with letters is -123, andasd122 is treated as 0.

1 intArraytoint (Char*str)2 {3     4     /*idea: Each traversal of a string is empty or the character is the end traversal of the character *10*/5     Long Longval =0;6     intFlag =0;7     Char*SRC =str;8     BOOLminus =false;9     /*Security*/Ten     if(str = =NULL) One     { A         return 0; -     } -     //the handling of positive and negative signs, whether plus or minus, takes a bit to add a pointer to the the     if(*SRC = ='-') -     { -Src++; -minus =true; +     } -     Else if(*SRC = ='+') +     { ASrc++; at     } -      -      while((*src! =' /') && (*SRC-'0'>=0) && (*SRC-'0'<=9))) -     { -         //multiply by bit by ten -Flag = minus? -1:1; inval = val *Ten+ flag* (*SRC-'0'); -         /*handling overflow problem: int max 0x7fffffff min 80000000*/ to         if((minus) && (val >0x7FFFFFFF)) || +(!minus) && (Val < (signedint)0x80000000))) -         { theval =0; *              Break; $         }Panax NotoginsengSrc++; -     } theprintf"val=%d",(int) val*flag); +     return(int) Val; A } the  

Integer conversion to string Inttoarray and string to integer Arraytoint implementation

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.