2007-02-25
一个将BIG5编码转换为GB2312编码的类
关键字: big5 gb2312 简体 繁体 转换RT,就是利用BIG5与GB2312码的对照表,代码本身很容易,只是找对照表花了我很长时间.
java 代码
- import java.io.*;
- /**
- *实现由BIG5编码到GB2312编码转换的工具类
- *@author: Eastsun
- *@version: 2007.2.24 v0.5
- */
- public class Big2Gb{
- private static final String tabFile ="bg-gb.tab";
- private static byte[] data;
- static{
- try{
- FileInputStream fis =new FileInputStream(tabFile);
- int len =fis.available();
- data =new byte[len];
- fis.read(data);
- fis.close();
- }catch(Exception ex){
- ex.printStackTrace();
- System.exit(1);
- }
- }
- /**
- *取得BIG5汉字big在data中的偏移
- */
- private static int indexOf(int big){
- int high =(big>>>8)&0xff;
- int low =big&0xff;
- high -= 0xa1;
- if(low<=0x7e) low -= 0x40;
- else low -= (0xa1 -0x7e -1) +0x40;
- return 2*(high*157+low);
- }
- /**
- *将保存在bs数字中的big5编码的字符串数据转换成gb2312编码的数据
- *注意:此方法将更改原先存储的数据
- *@param bs 需要转换的以big5编码的字符串数据
- *@return bs 经过转换的数据,保存在参数中的byte数组中
- */
- public static byte[] translateBig5ToGb(byte[] bs){
- int index =0;
- while(index<bs.length){
- int high =bs[index]&0xff;
- if(high>=0xa1&&high<=0xfe){
- index ++;
- if(index>=bs.length) break;
- int low =bs[index]&0xff;
- if(low<0x40||low>0xfe) continue;
- if(low>0x7e&&low<0xa1) continue;
- int offset =indexOf((high<<8)|low);
- bs[index-1] =data[offset];
- bs[index ] =data[offset+1];
- index++;
- }
- else index++;
- }
- return bs;
- }
- public static String translateBig5ToGb(String big){
- String result =null;
- try{
- byte[] bs =big.getBytes("big5");
- bs =translateBig5ToGb(bs);
- result =new String(bs,"gb2312");
- }catch(Exception e){
- }
- return result;
- }
- }
发表评论
- 浏览: 75643 次
- 性别:

- 来自: 天津

- 详细资料
搜索本博客
我的相册
6.5beta.PNG
共 61 张
共 61 张
最新评论
-
澄清:Java中只有按值传递 ...
Eastsun 写道归根究底,其实就是一个对“按引用传递”这个概念理解的问题。 ...
-- by suerzxt -
一个将BIG5编码转换为GB23 ...
谢谢了。呵呵
-- by zhangts8888 -
Java与Scala中的闭包
我说说我对闭包的理解,看看片面不,我理解的闭包是一个处理的输入和输出类型项相同, ...
-- by bloodrate -
Java与Scala中的闭包
刚刚试验了下java7: class A{ static { => in ...
-- by hity -
不要随便和老外说中文!!
。。。。。。。太强大了。。。。居然被gigix碰到真人真事了。。。
-- by sg552






评论排行榜