Fast integer to decimal conversion

后端 未结 4 1844
借酒劲吻你
借酒劲吻你 2021-01-03 09:36

Given an (unsigned) integer, what is the generally fastest way to convert it into a string that contains its decimal representation?

The naïve way of doing that

4条回答
  •  Happy的楠姐
    2021-01-03 10:10

    The MS version of printf does it the "naïve" way (after setting up a bunch of variables based on the optional flags):

                while (precision-- > 0 || number != 0) {
                    digit = (int)(number % radix) + '0';
                    number /= radix;                /* reduce number */
                    if (digit > '9') {
                        /* a hex digit, make it a letter */
                        digit += hexadd;
                    }
                    *text.sz-- = (char)digit;       /* store the digit */
                }
    

提交回复
热议问题