Re: Please Comment on this Integer to String Code.




manoj1...@xxxxxxxxx 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.

.



Relevant Pages

  • Newbie : friend function problem
    ... The problem relates to a Class Time and within it I have defined a friend ... When I program the function inline within the class definition everthing ... int minutes; ... void AddMin; ...
    (comp.lang.cpp)
  • Newbie : Problem with a friend function
    ... The problem relates to a Class Time and within it I have defined a friend ... When I program the function inline within the class definition everthing ... int minutes; ... void AddMin; ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Please Comment on this Integer to String Code.
    ... representation from base -32 to 32(32 since strtoul takes 32). ... void printBase ... int main ...
    (comp.lang.c)
  • Re: C coding guidelines
    ... did my graduation) or Kurukshetra University from where my friend did ... It was CLC who taught me, very first time in my life, to ... If you are writing code for embedded systems main will never return and it is appropriate to write void main. ... The C standard says that main will return an int. ...
    (comp.lang.c)
  • Please Comment on this Integer to String Code.
    ... representation from base -32 to 32(32 since strtoul takes 32). ... Used recursion so as not to crowd the logic. ... void printBase ... int main ...
    (comp.lang.c)