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