Code Conversion from java big5 to gb2312

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.