|
发表于 2013-7-20 16:30:31
|
显示全部楼层
网上找的随便看,没测试
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int H2D(char c){
- switch (toupper(c))
- {
- case 'A': return 10;
- case 'B': return 11;
- case 'C': return 12;
- case 'D': return 13;
- case 'E': return 14;
- case 'F': return 15;
- }
- return (c-'0');
- }
- unsigned short tbl[65535];
- void u2gb(unsigned char *u,unsigned char *gb){
- int i,j=0;
- unsigned char l,h;
- for(i=0;i<=strlen(u);){
- l=u[i++];
- h=u[i++];
- if(l==0 && h==0) break;
- if(l<0xA4 && h<=0){
- gb[j++]=l;
- continue;
- }
- gb[j++]=tbl[h*0x100+l]/0x100;
- gb[j++]=tbl[h*0x100+l]%0x100;
- }
- gb[j++]=0;
- }
- int init_u2gb(){
- FILE *fd;
- int size;
- char buffer[10];
- memset(tbl,'\0',sizeof(tbl));
- fd = fopen("code1.txt","r");
- if(fd==NULL) return -1;
- while(!feof(fd)){
- fread(buffer,sizeof(buffer),1,fd);
- tbl[H2D(buffer[0])*0x1000+H2D(buffer[1])*0x100+H2D(buffer[2])*0x10+H2D(buffer[3])] =
- H2D(buffer[5])*0x1000+H2D(buffer[6])*0x100+H2D(buffer[7])*0x10+H2D(buffer[8]);
- }
- close(fd);
- return 0;
- }
- int main(int argc, char *argv[])
- {
- unsigned char u[150],gb[150];
- memset(u,'\0',sizeof(u));
- memset(gb,'\0',sizeof(gb));
- init_u2gb();
- strcpy(u,"0");
- strcat(u,"\0\0");
- u2gb(u,gb);
- printf("%s\n",gb);
- scanf("%s",u);
- }
复制代码 |
|