java big5到gb2312的編碼轉(zhuǎn)換
更新時間:2008年12月20日 00:01:22 作者:
將保存在bs數(shù)字中的big5編碼的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成gb2312編碼的數(shù)據(jù) 此方法將更改原先存儲的數(shù)據(jù) 需要轉(zhuǎn)換的以big5編碼的字符串?dāng)?shù)據(jù)
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數(shù)字中的big5編碼的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成gb2312編碼的數(shù)據(jù)
*注意:此方法將更改原先存儲的數(shù)據(jù)
*@param bs 需要轉(zhuǎn)換的以big5編碼的字符串?dāng)?shù)據(jù)
*@return bs 經(jīng)過轉(zhuǎn)換的數(shù)據(jù),保存在參數(shù)中的byte數(shù)組中
*/
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;
}
}
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數(shù)字中的big5編碼的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成gb2312編碼的數(shù)據(jù)
*注意:此方法將更改原先存儲的數(shù)據(jù)
*@param bs 需要轉(zhuǎn)換的以big5編碼的字符串?dāng)?shù)據(jù)
*@return bs 經(jīng)過轉(zhuǎn)換的數(shù)據(jù),保存在參數(shù)中的byte數(shù)組中
*/
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;
}
}
相關(guān)文章
純JSP+DWR實現(xiàn)三級聯(lián)動下拉選擇菜單實現(xiàn)技巧
今天我做了一個dwr+jsp做的例子:純JSP+DWR實現(xiàn)三級聯(lián)動下拉選擇菜單,感興趣的朋友可以參考下,或許本文對你有所幫助2013-01-01JAVA/JSP學(xué)習(xí)系列之七(Orion下自定義Tag)
JAVA/JSP學(xué)習(xí)系列之七(Orion下自定義Tag)...2006-10-10