Please Comment on this Integer to String Code.

Discussion in 'C Programming' started by manoj1978@gmail.com, Mar 9, 2006.

  1. Guest

    Hi All,
    I wrote the following to convert an integer to its string
    representation from base -32 to 32(32 since strtoul takes 32).
    Used recursion so as not to crowd the logic.
    Please Comment on this code.
    Posting from google groups.hope it dont end up in many expert's kill
    file.

    #include <stdio.h>
    #include <stdlib.h>

    char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

    void print(int i,int base)
    {
    if (i == 0)return;
    if (i%base >= 0)
    {
    print(i / base, base);
    printf("%c",charTable[i % base]);
    } else {
    print(i / base + 1, base);
    printf("%c",charTable[i % base - base]);
    }
    }

    void printBase(int i,int base)
    {
    if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0))
    {
    printf("0\n");
    return;
    }

    if ((base > 0) && (i < 0))
    {
    printf("-");
    i = -i;
    }
    print(i, base);
    printf("\n");
    }

    int main(void)
    {
    int i;
    for(i = -55; i <= 55; i++)
    {
    printf("%3d = ", i);
    printBase(i,-10);
    }
    return 0;
    }

    Thanks and Regards,
    manoj.
     
    , Mar 9, 2006
    #1
    1. Advertising

  2. Guest

    wrote:

    > Hi All,
    > I wrote the following to convert an integer to its string
    > representation from base -32 to 32(32 since strtoul takes 32).

    One friend points out that it takes upto 36.
    > Used recursion so as not to crowd the logic.
    > Please Comment on this code.
    > Posting from google groups.hope it dont end up in many expert's kill
    > file.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";

    char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    >
    > void print(int i,int base)
    > {
    > if (i == 0)return;
    > if (i%base >= 0)
    > {
    > print(i / base, base);
    > printf("%c",charTable[i % base]);
    > } else {
    > print(i / base + 1, base);
    > printf("%c",charTable[i % base - base]);
    > }
    > }
    >
    > void printBase(int i,int base)
    > {
    > if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0))

    if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
    > {
    > printf("0\n");
    > return;
    > }
    >
    > if ((base > 0) && (i < 0))
    > {
    > printf("-");
    > i = -i;
    > }
    > print(i, base);
    > printf("\n");
    > }
    >
    > int main(void)
    > {
    > int i;
    > for(i = -55; i <= 55; i++)
    > {
    > printf("%3d = ", i);
    > printBase(i,-10);
    > }
    > return 0;
    > }
    >
    > Thanks and Regards,
    > manoj.
     
    , Mar 9, 2006
    #2
    1. Advertising

  3. Micah Cowan Guest

    writes:

    > Hi All,
    > I wrote the following to convert an integer to its string
    > representation from base -32 to 32(32 since strtoul takes 32).


    (You've already corrected this to 36 in another article.)

    > Used recursion so as not to crowd the logic.
    > Please Comment on this code.
    > Posting from google groups.hope it dont end up in many expert's kill
    > file.


    Have you checked the results of your program? They're completely
    wrong. A in base -10 should be identical to -(A in base 10).

    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV";
    >
    > void print(int i,int base)
    > {
    > if (i == 0)return;
    > if (i%base >= 0)
    > {
    > print(i / base, base);
    > printf("%c",charTable[i % base]);
    > } else {
    > print(i / base + 1, base);
    > printf("%c",charTable[i % base - base]);
    > }


    print(i / base, base); in both cases, and charTable[abs(i % base)].

    > }
    >
    > void printBase(int i,int base)
    > {
    > if ((abs(base) <= 1) || (abs(base) > 32) || (i == 0))
    > {
    > printf("0\n");
    > return;
    > }


    I'm not certain you should print 0 for bad bases: I would print out
    something that calls attention to the abuse of the function. In some
    situations, I might use an assert() for this: for others, I'd probably
    print something like "#ERR".

    Also, you should really use something other than a magic number for
    "32". You've already had to change this stuff once: what would've
    happened if you'd forgotten to change the number above? If you use

    (sizeof charTable) - 1

    then you'll never have to mess with it again. (you could remove the "-
    1" part if you use >= instead of >.)

    >
    > if ((base > 0) && (i < 0))
    > {
    > printf("-");
    > i = -i;
    > }


    Great. But what if base < 0 and i > 0?
    I think you want (base > 0) == (i < 0). And the inside will need some
    modification (remove the i = -i).

    > print(i, base);
    > printf("\n");
    > }
    >
    > int main(void)
    > {
    > int i;
    > for(i = -55; i <= 55; i++)
    > {
    > printf("%3d = ", i);
    > printBase(i,-10);
    > }
    > return 0;
    > }
    >
    > Thanks and Regards,
    > manoj.
     
    Micah Cowan, Mar 9, 2006
    #3
  4. Micah Cowan <> writes:
    [...]
    > Have you checked the results of your program? They're completely
    > wrong. A in base -10 should be identical to -(A in base 10).


    I haven't looked at how the program actually behaves, but A in base
    -10 is not the same as -(A in base 10).

    For a 3-digit number in base +10, the place values are +100, +10, and +1.
    For a 3-digit number in base -10, the place values are +100, -10, and +1
    (because (-10)**2 is +100, not -100).

    So, for example, 123 base 10 would be 283 in base -10.

    (Negative bases are silly, of course.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 9, 2006
    #4
  5. Micah Cowan Guest

    Keith Thompson <> writes:

    > Micah Cowan <> writes:
    > [...]
    > > Have you checked the results of your program? They're completely
    > > wrong. A in base -10 should be identical to -(A in base 10).

    >
    > I haven't looked at how the program actually behaves, but A in base
    > -10 is not the same as -(A in base 10).
    >
    > For a 3-digit number in base +10, the place values are +100, +10, and +1.
    > For a 3-digit number in base -10, the place values are +100, -10, and +1
    > (because (-10)**2 is +100, not -100).
    >
    > So, for example, 123 base 10 would be 283 in base -10.


    Doh! Of course you are right. I /knew/ I should've listen to that
    nagging feeling more closely.
     
    Micah Cowan, Mar 9, 2006
    #5
  6. Guest

    Micah Cowan wrote:
    > I'm not certain you should print 0 for bad bases: I would print out
    > something that calls attention to the abuse of the function. In some
    > situations, I might use an assert() for this: for others, I'd probably
    > print something like "#ERR".

    I want to use this later as a function returning a string.But will keep
    this in mind

    > Great. But what if base < 0 and i > 0?
    > I think you want (base > 0) == (i < 0). And the inside will need some
    > modification (remove the i = -i).

    I have a doubt here.If i is INT_MIN, will i = -i be undefined?

    Thanks and Regards,
    manoj.
     
    , Mar 10, 2006
    #6
  7. Micah Cowan Guest

    writes:

    > Micah Cowan wrote:
    > > I'm not certain you should print 0 for bad bases: I would print out
    > > something that calls attention to the abuse of the function. In some
    > > situations, I might use an assert() for this: for others, I'd probably
    > > print something like "#ERR".

    > I want to use this later as a function returning a string.But will keep
    > this in mind
    >
    > > Great. But what if base < 0 and i > 0?
    > > I think you want (base > 0) == (i < 0). And the inside will need some
    > > modification (remove the i = -i).

    > I have a doubt here.If i is INT_MIN, will i = -i be undefined?


    On a two's complement system, yeah. But ignore most of what I wrote,
    as I was trying to adapt it to my broken conception of how it was
    supposed to work.

    You can fix the i = -i thing by simply removing it, and adding the
    condition to print():

    if (base > 0 && i < 0)
    {
    print(-(i/base),base);
    printf("%c",charTable[-(i % base)]);
    }
    else if (i%base >= 0)
    {
    ...

    However, this condition will be true at most once, but evaluated every
    time, so you'd probably do better to move it to where you currently
    have the i = -i, and put the other print() currently in printBase()
    into an else clause.

    HTH,
    -Micah
     
    Micah Cowan, Mar 10, 2006
    #7
  8. Guest

    Keith Thompson wrote:
    > Micah Cowan <> writes:
    > [...]
    > > Have you checked the results of your program? They're completely
    > > wrong. A in base -10 should be identical to -(A in base 10).

    >
    > I haven't looked at how the program actually behaves, but A in base
    > -10 is not the same as -(A in base 10).
    >
    > For a 3-digit number in base +10, the place values are +100, +10, and +1.
    > For a 3-digit number in base -10, the place values are +100, -10, and +1
    > (because (-10)**2 is +100, not -100).
    >
    > So, for example, 123 base 10 would be 283 in base -10.
    >
    > (Negative bases are silly, of course.)


    They may be silly, but they are completely sound mathematically. The
    advantage to them is that you don't need a "-" sign to express numbers
    over the entire integer range.

    --
    Paul Hsieh
    http://www.pobox.com/~qed/
    http://bstring.sf.net/
     
    , Mar 10, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Analog_Guy
    Replies:
    6
    Views:
    563
    Andy Peters
    Jun 1, 2005
  2. Alec S.
    Replies:
    10
    Views:
    10,159
    Alec S.
    Apr 16, 2005
  3. Replies:
    58
    Views:
    1,035
    Ben Pope
    Mar 15, 2006
  4. Replies:
    102
    Views:
    1,743
    Ben Bacarisse
    Jul 23, 2008
  5. Randy Kramer
    Replies:
    12
    Views:
    376
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page