用c语言实现算术编码和解码
Turbo c 2.0编译通过
#include stdio.h
#include stdlib.h
#include string.h
#define LENGTH 100 /*字符串(编码前或编码后)的最大长度*/
/*编码*/
void encode(char *strsource);
/*解码*/
void decode(char *strcode);
void main()
{
char code[LENGTH]=”BILL GATES”;
encode(code);
printf(“\nencoded string is:%s\n”,code);
decode(code);
printf(“\ndecoded string is:%s\n”,code);
getch();
}
void encode(char *strsource){
char *p=strsource,tmp[LENGTH]={‘\0’},buffer[3];
while(*p){
itoa(*p++,buffer,10);
strcat(tmp,”%”);
strcat(tmp,buffer);
}
strcpy(strsource,tmp);
}
void decode(char *strcode){
int i=0;
char *p,*s=strcode,tmp[LENGTH]={‘\0’};
char *cSplit=”%”;
p=strtok(s,cSplit);
while(p)
{
tmp[i++] = atoi(p);
p=strtok(NULL,cSplit);
}
strcpy(strcode,tmp);
}
c语言 编码问题 求解
第一题:#includeintmain(intargc,char**argv){char*in=”in.txt”;char*out=”out.txt”;FILE*fpin,*fpout;intarr[50]={0};charch;intnum=0;fpin=fopen(in,”rb”);if(!fpin){printf(“Unabletoopen’%s’.\n”,in);return1;}while((ch=fgetc(fpin))!=EOF){if(ch==”||ch==’\n’){if(num=50){arr[num]+=1;}num=0;continue;}num=num*10+(ch-‘0′);}fclose(fpin);fpout=fopen(out,”wb”);if(!fpout){printf(“Unabletoopen’%s’.\n”,out);return1;}for(num=0;num50;++num)fprintf(fpout,”NU.:%dtimes:%d\n”,num,arr[num]);fclose(fpout);return0;}
C语言和汉字编码的问题 求教
为了区别于普通ASCII码,汉字编码一般在ASCII码之外,或者是两字节的第一字节在其之外,方法就是在汉字区位码上加一个固定的数字。但汉字有多套编码,互相不通用,可百度汉字编码了解详细信息。
为了显示汉字内码,可以在字符串中判断是否遇到汉字内码,如果是再按照编码规则解出汉字的区、位码,或者直接显示,
比如:char *str=”汉字编码”; printf(“%u,%u\n”,(unsigned int)str[0],(unsigned int)str[1]);
用内码显示汉字即为反方向赋值,如汉字“保”的国际码为B1A3H,那么构造字符串”保“的语句如下:
char *str[256]; unsigned int *p; p=str; *p=0xa3b1; //小端模式或者 str[0]=0xb1; str[1]=0xa3;