Re: Please Comment on this Integer to String Code.
- From: manoj1978@xxxxxxxxx
- Date: 9 Mar 2006 06:07:35 -0800
manoj1...@xxxxxxxxx wrote:
Hi All,One friend points out that it takes upto 36.
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.char charTable[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
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";
if ((abs(base) <= 1) || (abs(base) > 36) || (i == 0))
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.
.
- References:
- Please Comment on this Integer to String Code.
- From: manoj1978
- Please Comment on this Integer to String Code.
- Prev by Date: Please Comment on this Integer to String Code.
- Next by Date: errno questions (leading to a slightly OT follow-up question)
- Previous by thread: Please Comment on this Integer to String Code.
- Next by thread: Re: Please Comment on this Integer to String Code.
- Index(es):
Relevant Pages
|