数字与字符串之间的转换

    xiaoxiao2026-02-25  8

    1 atoi字符串转换成整型数

    int Myatoi(char *str) { if(str==NULL) { printf("Invalid Input"); return -1; } while(*str==' ') { str++; } while((*str==(char)0xA1) && (*(str+1)==(char)0xA1)) { str+=2; } int nSign=(*str=='-')?-1:1; if(*str=='+' || *str=='-') { str++; } int nResult=0; while(*str>'0' && *str<'9') { nResult=nResult*10+(*str-'0'); str++; } return nResult*nSign; }

    2 整型数转换成字符串

    #include <stdio.h> char *Myitoa(int num) { char str[1024]; int sign=num,i=0,j=0; char temp[11]; if(sign<0) { num=-num; } do { temp[i]=num%10+'0'; num/=num; i++; }while(num>0); if(sign<0) { temp[i++]='-'; } temp[i]='\0'; i--; while(i>=0) { str[j]=temp[i]; j++; i--; } str[j]='\0'; return str; }
    转载请注明原文地址: https://ju.6miu.com/read-1307378.html
    最新回复(0)