javabig5到gb2312的编码转换
2023-06-13 09:14:07 时间
packagecom.Big5ToUTF8;
importjava.io.*;
publicclassBig5Tran{
privatestaticfinalStringtabFile="bg-gb.tab";
privatestaticbyte[]data;
static{
try{
FileInputStreamfis=newFileInputStream(tabFile);
intlen=fis.available();
data=newbyte[len];
fis.read(data);
fis.close();
}catch(Exceptionex){
ex.printStackTrace();
System.exit(1);
}
}
/**
*取得BIG5汉字big在data中的偏移
*/
privatestaticintindexOf(intbig){
inthigh=(big>>>8)&0xff;
intlow=big&0xff;
high-=0xa1;
if(low<=0x7e)low-=0x40;
elselow-=(0xa1-0x7e-1)+0x40;
return2*(high*157+low);
}
/**
*将保存在bs数字中的big5编码的字符串数据转换成gb2312编码的数据
*注意:此方法将更改原先存储的数据
*@parambs需要转换的以big5编码的字符串数据
*@returnbs经过转换的数据,保存在参数中的byte数组中
*/
publicstaticbyte[]translateBig5ToGb(byte[]bs){
intindex=0;
while(index<bs.length){
inthigh=bs[index]&0xff;
if(high>=0xa1&&high<=0xfe){
index++;
if(index>=bs.length)break;
intlow=bs[index]&0xff;
if(low<0x40||low>0xfe)continue;
if(low>0x7e&&low<0xa1)continue;
intoffset=indexOf((high<<8)|low);
bs[index-1]=data[offset];
bs[index]=data[offset+1];
index++;
}
elseindex++;
}
returnbs;
}
publicstaticStringtranslateBig5ToGb(Stringbig){
Stringresult=null;
try{
byte[]bs=big.getBytes("big5");
bs=translateBig5ToGb(bs);
result=newString(bs,"gb2312");
}catch(Exceptione){
}
returnresult;
}
}
importjava.io.*;
publicclassBig5Tran{
privatestaticfinalStringtabFile="bg-gb.tab";
privatestaticbyte[]data;
static{
try{
FileInputStreamfis=newFileInputStream(tabFile);
intlen=fis.available();
data=newbyte[len];
fis.read(data);
fis.close();
}catch(Exceptionex){
ex.printStackTrace();
System.exit(1);
}
}
/**
*取得BIG5汉字big在data中的偏移
*/
privatestaticintindexOf(intbig){
inthigh=(big>>>8)&0xff;
intlow=big&0xff;
high-=0xa1;
if(low<=0x7e)low-=0x40;
elselow-=(0xa1-0x7e-1)+0x40;
return2*(high*157+low);
}
/**
*将保存在bs数字中的big5编码的字符串数据转换成gb2312编码的数据
*注意:此方法将更改原先存储的数据
*@parambs需要转换的以big5编码的字符串数据
*@returnbs经过转换的数据,保存在参数中的byte数组中
*/
publicstaticbyte[]translateBig5ToGb(byte[]bs){
intindex=0;
while(index<bs.length){
inthigh=bs[index]&0xff;
if(high>=0xa1&&high<=0xfe){
index++;
if(index>=bs.length)break;
intlow=bs[index]&0xff;
if(low<0x40||low>0xfe)continue;
if(low>0x7e&&low<0xa1)continue;
intoffset=indexOf((high<<8)|low);
bs[index-1]=data[offset];
bs[index]=data[offset+1];
index++;
}
elseindex++;
}
returnbs;
}
publicstaticStringtranslateBig5ToGb(Stringbig){
Stringresult=null;
try{
byte[]bs=big.getBytes("big5");
bs=translateBig5ToGb(bs);
result=newString(bs,"gb2312");
}catch(Exceptione){
}
returnresult;
}
}
相关文章
- 关于使用LayoutParams清除设置以及DateFormat无法正确转换格式化日期的问题
- java编译报错提示编码GBK的不可映射字符啥意思_java字符串编码转换
- 【java实现网址转换为二维码】「建议收藏」
- MacX Video Converter Pro for mac(视频转换工具)
- python代码实例大小写转换,首字母大写,去除特殊字符详解编程语言
- JAVA实现图片与base64字符串之间的转换详解编程语言
- 使用QString类进行编码转换
- 转换MySQL命令行实现编码转换(mysql命令行编码)
- Linux下快速转换UTF8编码(linux转utf8)
- MySQL如何将字符转换为数字(mysql 字符 转数字)
- MySQL如何支持GB2312编码(mysql中gb2312)
- Oracle中优雅实现字符串转换(oracle中转换字符串)
- 简单的Html转换UBB的程序
- asp下实现UrlEncoding转换编码的代码
- php数组编码转换示例详解
- jsunicode编码解析关于数据转换为中文的两种方法
- PHP学习笔记之字符串编码的转换和判断
- Android处理图像数据转换的各种方法