Conversion between simplified and traditional strings

Source: Internet
Author: User
Package COM. newbee. demo;/* gb2big5-decompiled by jode * visit http://jode.sourceforge.net/*/import Java. io. bufferedoutputstream; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. fileoutputstream; import Java. io. inputstream; import Java. util. properties; public class gb2big5 {Private Static gb2big5 pinstance = NULL; private string s_big5tablefile = NULL; private St Ring s_gbtablefile = NULL; private byte [] B _big5table = NULL; private byte [] B _gbtable = NULL; private gb2big5 (string sgbtablefile, string sbig5tablefile) throws limit {s_big5tablefile = sbig5tablefile; s_gbtablefile = sgbtablefile; If (null = B _gbtable) B _gbtable = partition (sgbtablefile); If (null = B _big5table) B _big5table = getbytesfromfile (sbig5tablefile); If (null = B _gbtable) Throw new nullpointerexception ("No GB table can be load"); If (null = B _big5table) throw new nullpointerexception ("No big5 table can be load ");} // character conversion table public static synchronized gb2big5 getinstance () {return getinstance ("/COM/Newbee/demo/gb-big5.table", "/COM/Newbee/demo/big5-gb.table ");} private Static synchronized gb2big5 getinstance (string sgbtablefile, string sbig5tablefile) {If (null = pinsta NCE) {try {pinstance = new gb2big5 (sgbtablefile, sbig5tablefile);} catch (exception e) {system. err. println (E. tostring (); pinstance = NULL;} return pinstance;} protected synchronized void resetbig5char (string gbchar, string big5char) throws exception {byte [] Text = new string (gbchar. getbytes (), system. getproperty ("file. encoding ")). getbytes ("GBK"); byte [] textbig5 = new string (big5char. getbytes (), syst Em. getproperty ("file. encoding ")). getbytes ("big5"); int max = text. length-1; int h = 0; int L = 0; int p = 0; int B = 256; byte [] Big = new byte [2]; for (INT I = 0; I <Max; I ++) {H = text [I]; If (H <0) {H = B + h; L = text [I + 1]; If (L <0) L = B + Text [I + 1]; If (H! = 161 | L! = 64) {P = (H-160) * 510 + (L-1) * 2; B _gbtable [p] = textbig5 [I]; B _gbtable [p + 1] = textbig5 [I + 1];} I ++;} bufferedoutputstream pwriter = new bufferedoutputstream (New fileoutputstream (s_gbtablefile); pwriter. write (B _gbtable, 0, B _gbtable.length); pwriter. close ();} protected synchronized void resetgbchar (string big5char, string gbchar) throws exception {byte [] textgb = new string (gbchar. getbytes (), System. getproperty ("file. encoding ")). getbytes ("GBK"); byte [] Text = new string (big5char. getbytes (), system. getproperty ("file. encoding ")). getbytes ("big5"); int max = text. length-1; int h = 0; int L = 0; int p = 0; int B = 256; byte [] Big = new byte [2]; for (INT I = 0; I <Max; I ++) {H = text [I]; If (H <0) {H = B + h; L = text [I + 1]; If (L <0) L = B + Text [I + 1]; If (H! = 161 | L! = 64) {P = (H-160) * 510 + (L-1) * 2; B _big5table [p] = textgb [I]; B _big5table [p + 1] = textgb [I + 1];} I ++;} bufferedoutputstream pwriter = new bufferedoutputstream (New fileoutputstream (s_big5tablefile); pwriter. write (B _big5table, 0, B _big5table.length); pwriter. close ();} public byte [] gb2big5 (string instr) throws exception {If (null = instr | instr. length () <= 0) Return "". getbytes (); byte [] Text = New String (instr. getbytes (), system. getproperty ("file. encoding ")). getbytes ("GBK"); int max = text. length-1; int h = 0; int L = 0; int p = 0; int B = 256; byte [] Big = new byte [2]; for (INT I = 0; I <Max; I ++) {H = text [I]; If (H <0) {H = B + h; L = text [I + 1]; If (L <0) L = B + Text [I + 1]; If (H = 161 & l = 64) big [0] = big [1] = (byte) (161-B); else {P = (H-160) * 510 + (L-1) * 2; try {big [0] = (Byte) (B _gbtable [p]-B);} catch (exception e) {big [0] = (byte) 45;} Try {big [1] = (byte) (B _gbtable [p + 1]-B);} catch (exception e) {big [1] = (byte) 45 ;}} text [I] = big [0]; text [I + 1] = big [1]; I ++;} return text;} public byte [] big52gb (string instr) throws exception {If (null = instr | instr. length () <= 0) return New byte [0]; byte [] Text = new string (instr. getbytes (), system. getproperty ("file. ENCO Ding ")). getbytes ("big5"); int max = text. length-1; int h = 0; int L = 0; int p = 0; int B = 256; byte [] Big = new byte [2]; for (INT I = 0; I <Max; I ++) {H = text [I]; If (H <0) {H = B + h; L = text [I + 1]; If (L <0) L = B + Text [I + 1]; If (H = 161 & l = 161) {big [0] = (byte) (161-B); big [1] = (byte) (64-B);} else {P = (H-160) * 510 + (L-1) * 2; try {big [0] = (byte) (B _big5table [p]-B);} catch (Exception e) {big [0] = (byte) 45;} Try {big [1] = (byte) (B _big5table [p + 1]-B );} catch (exception e) {big [1] = (byte) 45 ;}} text [I] = big [0]; text [I + 1] = big [1]; I ++ ;}} return text;} Private Static byte [] getbytesfromfile (string infilename) {byte [] is; try {inputstream in = gb2big5. class. getresourceasstream (infilename); bytearrayoutputstream out = new bytearrayoutputstream (1024); byte [] bt = new byte [1 024]; for (INT I = 0; (I = in. Read (BT ))! =-1;) {out. write (BT, 0, I);} In. close (); is = out. tobytearray ();} catch (exception e) {e. printstacktrace (); return NULL;} return is;} public static void main (string [] ARGs) throws exception {gb2big5 pTMP = gb2big5. getinstance (); system. out. println ("I'm a traditional Chinese character =>" + new string (pTMP. big52gb ("I'm a traditional Chinese character"), "GBK"); system. out. println ("I Am a simplified Chinese character =>" + new string (pTMP. gb2big5 ("I Am a simplified Chinese character"), "big5 "));}}

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.