decimal to given base

Sep 9, 2013 at 5:03am
//This function can be used to convert decimal to given base
void To(long long num,char *buff,int base)
{
if(buff==NULL) return;
long long m=0,no=num,i=1;

while((no/=base)>0) i++;
buff[i]='\0';

no=num;
while(no>0)
{
m=no%base;
no=no/base;
buff[--i]=(m>9)?((base==16)?('A' + m - 10):m):m+48;
}
}
Sep 9, 2013 at 5:10am
what is the problem ?
Sep 9, 2013 at 1:13pm
It doesn't handle negative numbers, or zero.

A value of base greater than 10, such as base==12 gives results which are not readable. That's because the character output when base is greater than 10 but not equal to 16 is just m. It might be better to use 'A' + m - 10 for any value of base which is greater than 10.
Topic archived. No new replies allowed.