整数向字符串转换

2018-03-05  本文已影响0人  32633c18e222

代码来自陈硕


const char*convert(char buf[], int value) {

static  char digits[19] =

{

'9', '8','7','6','5','4','3','2','1','0',

                  '1','2','3','4','5','6','7','8','9'

          };

  static const char* zero = digits +9;

  int i = value;

  char *p = buf;

  do {

// lsd - least significant digit

    int lsd = i %10;

    i /=10;

    *p++ = zero[lsd];

  }while (i !=0);

  if (value <0) {

*p++ ='-';

  }

*p ='\0';

  reverse(buf, p);

  return p;

}

上一篇下一篇

猜你喜欢

热点阅读