Listing 2: Signed integer conversion

/* /////////////////////////////////////////////////////////////
 *
 * ...
 *
 * Extract from stlsoft_integer_to_string.h
 *
 * www:        http://www.synesis.com.au/stlsoft
 *             http://www.stlsoft.org/
 *
 * Copyright (C) 2002, Synesis Software Pty Ltd.
 * (Licensed under the Synesis Software Standard Source License:
 *  http://www.synesis.com.au/licenses/ssssl.html)
 *
 * ...
 *
 * ////////////////////////////////////////////////////////// */

...

template <class C, class I>
inline const C *signed_integer_to_string(   C       *buf,
                                            size_t  cchBuf,
                                            I       i)
{
    C   *psz    =   buf + cchBuf - 1;   // Set psz to last char

    *psz = 0;                           // Set terminating null

    if(i < 0)
    {
        do
        {
            signed      lsd = i % 10;   // Determine least 
                                        // significant digit

            i /= 10;                    // Deal with next most
                                        // significant digit

            --psz;                      // Move back

            *psz = get_digit_character<C>()[lsd]; // Place digit

        } while(i != 0);

        *(--psz) = C('-');              // Prepend minus sign
    }
    else
    {
        do
        {
            signed      lsd = i % 10;   // Determine least 
                                        // significant digit.

            i /= 10;                    // Deal with next most
                                        // significant digit

            --psz;                      // Move back

            *psz = get_digit_character<C>()[lsd]; // Place digit

        } while(i != 0);
    }

    return psz;
}
— End of Listing —