// char*itoa(intvalue,char*string,intradix); linux库中无此函数 // int value 被转换的整数,char *string 转换后储存的字符数组, // int radix 转换进制数,如2,8,10,16 进制等 #include <stdio.h> #include <stdlib.h> #include <ctype.h>int my1atoi(const char *str) { int value = 0; int symble = 1; if( NULL == str) { printf("NULL == str\n"); return -1; } while(0 != isspace(*str)) { str++; } if('-' == *str) { symble = -1; str++; } else if( '+'==*str) { str++; } while('0'<=*str && *str<='9') { value = value*10 + (*str - '0'); str++; } return value*symble; } int main(int argc, char * argv [ ]) { int n=0,m; //n = myatoi("-A"); const char a[]=" -11-00"; const char b[]="-200"; char c[20]={0}; char d[20]={0}; n = atoi(a); m = atoi(b); printf("%d %d\n",n,m); n = my1atoi(a); m = my1atoi(b); printf("%d %d\n",n,m); return 0; } |
|