当前位置:主页 > 查看内容

java big5到gb2312的编码转换

发布时间:2021-07-15 00:00| 位朋友查看

简介:package com.Big5ToUTF8; import java.io.*; public class Big5Tran { private static final String tabFile ="bg-gb.tab"; private static byte[] data; static{ try{ FileInputStream fis =new FileInputStream(tabFile); int len =fis.available(); data……
package com.Big5ToUTF8;
import java.io.*;
public class Big5Tran {
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;
}
}

本文转载自网络,原文链接:https://m.jb51.net/article/16864.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:java Lucene 中自定义排序的实现 下一篇:没有了

推荐图文


随机推荐