数字转字符串实现

int convert(char buf[], int value)
{
    constexpr char digits[] = {'9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
                               '1', '2', '3', '4', '5', '6', '7', '8', '9'};
    const char* zero = digits + 9;
    int   i = value;
    char* p = buf;
    do{
        int pushed_nunmber_index = i % 10;
        i /= 10;
        *p++ = zero[pushed_nunmber_index];
    }while(i != 0);
    if (value < 0){
        *p++ = '-';
    }
    *p = '\0';
    std::reverse(buf, p);
    return static_cast<int>(p - buf);
}

 

posted @ 2016-02-11 18:04  wu_overflow  阅读(251)  评论(0编辑  收藏  举报