BIG5码转换为GB2312码的方法

	繁体字一般都使用BIG5编码,而简体中文一般都使用GB2312编码,要将BIG5编码的繁体字转换为GB2312编码的简体字时,就要经过一步转换过程,过程如下

1、生成常用繁体字的BIG5码表,用如下c代码可生成BIG5码表文件

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

int main(){
	FILE * codefile;
	int i, j;
	codefile = fopen("table.txt", "w+b");
	for (i=0xa1; i <= 0xf9; i++){
		for(j = 0x40; j <= 0x7e; j++){
			fwrite(&i, 1, 1, codefile);
			fwrite(&j, 1, 1, codefile);
		}

		for(j = 0xa1; j <= 0xfe; j++){
			fwrite(&i, 1, 1, codefile);
			fwrite(&j, 1, 1, codefile);
		}
	}
	fclose(codefile);
	return 0;
}
生成的文件保存在table.txt文件中

2、将生成的文件用内码转换精灵工具转换为相应的GB2312码表文件, 这个文件用于后续的查表

3、编写代码读取文件中的BIG5码,然后根据步骤2中转换得到的码表文件进行查找得到相应繁体的简体GB2312码,执行转换功能的c代码如下:

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

int main(){
	int que, wei;
	FILE *sourcefile;
	FILE *tabfile;
	FILE *destfile;

	//BIG5 码文件
	sourcefile = fopen("big.txt", "r+b");

	//码表文件
	tabfile = fopen("table_BIG2GB.txt", "r+b");

	//转换生成的GB码文件
	destfile = fopen("gb.txt","w+b");

	while (!feof(sourcefile)){
		fread(&que, 1, 1, sourcefile);
		printf("que=0x%x\n", que);
		if (feof(sourcefile)){
			break; 
		}

		if (que >= 0xa1 && que <=0xfe)
		{
			//叛断是否汉字(BIG5编码)
			fread(&wei, 1, 1, sourcefile);
			printf("wei=0x%x\n", wei);
			if (wei < 0xa1) 
				wei = wei - 0x40;
			if (wei >= 0xa1) 
				wei = wei - 0xa1 + 0x7e - 0x40 + 1;
			fseek(tabfile, 2 * ((que -0xa1) * (0xfe - 0xa1 + 1 + 0x7e - 0x40 + 1 ) + wei), SEEK_SET);
			fread(&que, 1, 1, tabfile);
			fread(&wei, 1, 1, tabfile);
			fwrite(&que, 1, 1, destfile);
			printf("after convert, que=0x%x\n", que);
			fwrite(&wei, 1, 1, destfile);
			printf("after convert, wei=0x%x\n", wei);
		}
		else
		{
			fwrite(&que, 1, 1, destfile); //处理英文
		}
	}

	fclose(sourcefile);
	fclose(tabfile);
	fclose(destfile);
	return 0;
}
4、测试,使用notepad++文本编辑工具编辑几个繁体字,并使其编码为BIG5,该工具有这个功能,并将文件命名为big.txt。内码转换精灵转换得到的码表文件

命名为table_BIG2GB.txt,然后使用步骤3中的代码进行转换,即可得到big.txt文件中繁体字的相应简体
本代码已经经过验证

我的资源列表中有相应的资源

http://download.csdn.net/detail/dengchendeng/6420109


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值