Please Comment on this Integer to String Code.

M

manoj1978

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

manoj1978

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))
 
M

Micah Cowan

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

Keith Thompson

Micah Cowan said:
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.)
 
M

Micah Cowan

Keith Thompson said:
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.
 
M

manoj1978

Micah said:
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.
 
M

Micah Cowan

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

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
 
W

websnarf

Keith said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top