Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Please Comment on this Integer to String Code.

Reply
Thread Tools

Please Comment on this Integer to String Code.

 
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-09-2006
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.

 
Reply With Quote
 
 
 
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-09-2006

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.


 
Reply With Quote
 
 
 
 
Micah Cowan
Guest
Posts: n/a
 
      03-09-2006
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-09-2006
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) kst- <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.
 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-09-2006
Keith Thompson <kst-> 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.
 
Reply With Quote
 
manoj1978@gmail.com
Guest
Posts: n/a
 
      03-10-2006

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.

 
Reply With Quote
 
Micah Cowan
Guest
Posts: n/a
 
      03-10-2006
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
 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      03-10-2006
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
is there a way to AutoParse a string to another type - e.g. if aDate format then date, else if integer than Integer etc ????? Greg Hauptmann Ruby 6 08-06-2008 04:52 PM
Comment on trim string function please swengineer001@gmail.com C Programming 102 07-23-2008 08:27 PM
Change a string to an integer, report an error if the string does not represent an integer? Randy Kramer Ruby 12 10-25-2007 09:56 PM
Please comment on my integer to string code. manoj1978@gmail.com C++ 58 03-15-2006 02:44 PM
? ELSE Conditional Comment / Using Conditional Comments Inside Other Tags To Comment Out Attributes Alec S. HTML 10 04-16-2005 02:21 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57