My favorites | Sign in
Project Home Wiki Issues Source
Project Information
Members

Cross platform itoa function with better performance. Support linux and windows and others.

It used 180 ms in my test, while itoa of MFC use 320 ms.

Please refer to: http://blog.csdn.net/wwwsq/archive/2010/01/05/5137006.aspx

//return the length of result string. support only 10 radix for easy use and better performance
int my_itoa(int val, char* buf)
{
    const unsigned int radix = 10;

    char* p;
    unsigned int a;        //every digit
    int len;
    char* b;            //start of the digit char
    char temp;
    unsigned int u;

    p = buf;

    if (val < 0)
    {
        *p++ = '-';
        val = 0 - val;
    }
    u = (unsigned int)val;

    b = p;

    do
    {
        a = u % radix;
        u /= radix;

        *p++ = a + '0';

    } while (u > 0);

    len = (int)(p - buf);

    *p-- = 0;

    //swap
    do
    {
        temp = *p;
        *p = *b;
        *b = temp;
        --p;
        ++b;

    } while (b < p);

    return len;
}

 
Powered by Google Project Hosting