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