Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)

 manoj1978@gmail.com 03-09-2006 02:02 PM

Please Comment on this Integer to String Code.

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.
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.

 manoj1978@gmail.com 03-09-2006 02:07 PM

Re: Please Comment on this Integer to String Code.

manoj1...@gmail.com 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.

 Micah Cowan 03-09-2006 09:40 PM

Re: Please Comment on this Integer to String Code.

manoj1978@gmail.com 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
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.

 Keith Thompson 03-09-2006 11:23 PM

Re: Please Comment on this Integer to String Code.

Micah Cowan <micah@cowan.name> 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) kst-u@mib.org <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.

 Micah Cowan 03-09-2006 11:29 PM

Re: Please Comment on this Integer to String Code.

Keith Thompson <kst-u@mib.org> writes:

> Micah Cowan <micah@cowan.name> 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.

 manoj1978@gmail.com 03-10-2006 04:46 AM

Re: Please Comment on this Integer to String Code.

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.

 Micah Cowan 03-10-2006 08:06 PM

Re: Please Comment on this Integer to String Code.

manoj1978@gmail.com 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

 websnarf@gmail.com 03-10-2006 11:32 PM

Re: Please Comment on this Integer to String Code.

Keith Thompson wrote:
> Micah Cowan <micah@cowan.name> 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/

 All times are GMT. The time now is 01:05 AM.