2014年05月19日

来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/03/29 22:22

practice 3-5: Write a function itob(n,s,b),that convertsthe integer n into a base b character representation in the strings.In particular,itob(n,s,16) formats n as a hexadecimal integer ins.

#include

void itob(int n,char s[],int b);
void reverse(char s[]);
main()
{
 int number=17,b1=2,b2=8,b3=16;
 char str[10];

 printf("original digit:%d\n",number);
 itob(number,str,b1);
 printf("change:%s",str);
 


 return 0;
}

void itob(int n,char s[],int b)
{
 int i,sign;
 static chardigits[]="0123456789ABCDEFGHIJKLMNOPQRSTYVWXYZ";
 char t[10];

 if((sign=n)<0)
  n=-n;
 i=0;
 do{
  s[i++]=digits[n%b];
  }while((n/=b)>0);
 if(sign<0)
  s[i++]='-';
 s[i]='\0';

 reverse(s);
}
void reverse(char s[])
{
 int c,i,j;
 for(i=0,j=strlen(s)-1;i
 {
  c=s[i];s[i]=s[j];s[j]=c;
 }
}

 

CONCLUSION:This procesure is refer to the rightanswer,but the base idea is not changed.don't amuse the rightanswer,it can be a helper,but it also can be adesdroyer.

0 0