数字转化成字符串C语言

#include <stdio.h>
void Myitoa(int,char *);
int getnumberLength(int);
int main(){
    char buf[10];
    int i = 510345;
    Myitoa(i, buf);
    printf("%s", buf);


    getchar();
    return 0;

}
void Myitoa(int a, char *p){
    int numberlength = getnumberLength(a);
    int flag = 1;
    if (a<0){
        flag = -1;
        a = -a;
    }
    *(p + numberlength) = 0;
    while (numberlength-- > 0){
        p[numberlength] = a % 10 + 0x30;
        a = a / 10;
    }
    if (flag < 0){
        p[0] = (char)45;
    }
}
int getnumberLength(int a){
    int result = 1;
    if (a < 0){
        a = -a;
        result ++;
    }
    while (a /= 10){
        result++;
    }
    return result;
}

 

posted on 2016-03-01 09:46  木鸟飞  阅读(320)  评论(0编辑  收藏  举报

导航