fb39ca4 / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Bug in PrintUnsigned #73

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. int x=0;
2. printf("%02x", x);
3.

What is the expected output? What do you see instead?
expect "00", see "000"

What version of the product are you using? On what operating system?
latest

Please provide any additional information below.
suggested fix:
void PrintUnsigned(unsigned long Num, unsigned int Base, int FieldWidth, 
int ZeroPad, int LeftJustify, struct OutputStream *Stream)
{
    char Result[33];
    int ResPos = sizeof(Result);

    Result[--ResPos] = '\0';
    if (Num == 0)
    {
        Result[--ResPos] = '0';
    }
    else while (Num > 0)
    {
        unsigned long NextNum = Num / Base;
        unsigned long Digit = Num - NextNum * Base;
        if (Digit < 10)
            Result[--ResPos] = '0' + Digit;
        else
            Result[--ResPos] = 'a' + Digit - 10;

        Num = NextNum;
    }

    if (FieldWidth > 0 && !LeftJustify)
        PrintRepeatedChar(ZeroPad ? '0' : ' ', FieldWidth - (sizeof(Result) 
- 1 - ResPos), Stream);

    PrintStr(&Result[ResPos], Stream);

    if (FieldWidth > 0 && LeftJustify)
        PrintRepeatedChar(' ', FieldWidth - (sizeof(Result) - 1 - ResPos), 
Stream);
}

Original issue reported on code.google.com by casey34...@gmail.com on 25 Feb 2010 at 6:09

GoogleCodeExporter commented 9 years ago
Thanks for the excellent bug report.

Fixed in revision #406

Original comment by zik.sale...@gmail.com on 25 Feb 2010 at 8:12