Package com. Big5ToUTF8;
Import java. io .*;
Public class Big5Tran {
Private static final String tabFile = "bg-gb.tab ";
Private static byte [] data;
Static {
Try {
FileInputStream FCM = new FileInputStream (tabFile );
Int len = FCM. available ();
Data = new byte [len];
FS. read (data );
FCM. close ();
} Catch (Exception ex ){
Ex. printStackTrace ();
System. exit (1 );
}
}
/**
* Get the BIG5 Chinese Character big offset in 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 );
}
/**
* Convert big5 encoded string data stored in bs numbers to gb2312 encoded data.
* Note: This method changes the data originally stored.
* @ Param bs string data to be converted in big5 Encoding
* @ Return bs converted data is stored in the byte array of the Parameter
*/
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;
}
}