itob

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void itob(int n,char s[],int b);
void reverse(char s[]);
void reverse(char s[])
{
int i=0;
int j=strlen(s)-1;
while(i<j)
{
int temp=s[i];
s[i]=s[j];
s[j]=temp;
i++;
j--;
}
}

void itob(int n,char s[],int b)
{
static char digits[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int sign=n;
int i=0;
if(b<2||b>36)
{
fprintf(stderr,"Can not support base %d\n",b);
exit(EXIT_FAILURE);
}
do{
s[i++]=digits[abs(n%b)];
}while(n/=b);
if(sign<0)
s[i++]='-';
s[i]='\0';
reverse(s);
}

int main()
{
char buffer[10];
int i;
for(i=2;i<=20;i++){
itob(255,buffer,i);
printf("Decimal 255 in base %-2d: %s\n",i,buffer);
}
return 0;
}

你可能感兴趣的:(J#)