国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

java big5到gb2312的編碼轉換

2024-09-05 00:21:22
字體:
來源:轉載
供稿:網友
將保存在bs數字中的big5編碼的字符串數據轉換成gb2312編碼的數據 此方法將更改原先存儲的數據 需要轉換的以big5編碼的字符串數據

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;
}
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广灵县| 梅河口市| 碌曲县| 开封县| 额济纳旗| 镇江市| 云霄县| 太仓市| 泾川县| 驻马店市| 柯坪县| 卢湾区| 广昌县| 峨眉山市| 东乡县| 夏邑县| 四子王旗| 中牟县| 吉首市| 金山区| 枣强县| 神农架林区| 图片| 祁阳县| 西青区| 登封市| 益阳市| 特克斯县| 中山市| 汝州市| 凤凰县| 珠海市| 上杭县| 襄垣县| 桑日县| 日照市| 长阳| 罗源县| 武功县| 仙居县| 栾川县|