Java program mapping gb2312 to Unicode

Source: Internet
Author: User

In order to produce the mapping tables for this book, I wrote the following Java program, gb2312unicode. java.

The output of this program is presented in the next 3 chapters.

/*** Gb2312unicode. java * copyright (c) 1997-2003 by Dr. herong Yang */import Java. io. *; import Java. NIO. *; import Java. NIO. charset. *; Class gb2312unicode {static outputstream out = NULL; static char hexdigit [] = {'0', '1', '2', '3', '4 ', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E ', 'F'}; static int B _out [] = {201,267,279,293,484,587,625,657,734,782,827,874,901,980,559 0}; static int e_out [] = {216,268,280,294,494,594,632,694,748,794,836,894,903,994,559 4}; public static void main (string [] ARGs) {try {out = new fileoutputstream ("gb2312_unicode.gb"); writecode (); out. close ();} catch (ioexception e) {system. out. println (E. tostring () ;}} public static void writecode () throws ioexception {Boolean reserved = false; string name = NULL; // gb2312 is not supported by JDK. so I am using GB K. charsetdecoder gbdc = charset. forname ("GBK "). newdecoder (); charsetencoder uxec = charset. forname ("UTF-16BE "). newencoder (); charsetencoder u8ec = charset. forname ("UTF-8 "). newencoder (); bytebuffer gbbb = NULL; bytebuffer uxbb = NULL; bytebuffer u8bb = NULL; charbuffer cb = NULL; int COUNT = 0; For (INT I = 1; I <= 94; I ++) {// defining row settings if (I> = 1 & I <= 9) {reserved = false; name = "Graph IC symbols ";} else if (I >=10 & I <= 15) {reserved = true; name =" Reserved ";} else if (I >=16 & I <= 55) {reserved = false; name = "Level 1 characters ";} else if (I >=56 & I <= 87) {reserved = false; name = "Level 2 characters ";} else if (I> = 88 & I <= 94) {reserved = true; name = "Reserved" ;}// writing row title writeln (); writestring ("<p>"); writenumber (I); writestring ("row:" + name); writeln (); Writestring ("</P>"); writeln (); If (! Reserved) {writeln (); writeheader (); // looping through all characters in one row for (Int J = 1; j <= 94; j ++) {byte HI = (byte) (0xa0 + I); byte Lo = (byte) (0xa0 + J); If (validgb (I, j) {// getting GB, UTF-16BE, UTF-8 codes gbbb = bytebuffer. wrap (New byte [] {hi, lo}); try {cb = gbdc. decode (gbbb); uxbb = uxec. encode (CB); CB. rewind (); u8bb = u8ec. encode (CB);} catch (charactercodingexception e) {cb = NULL; uxbb = NULL; u8bb = NULL;} else {cb = NULL; uxbb = NULL; u8bb = NULL;} writenumber (I); writenumber (j ); writestring (""); If (CB! = NULL) {writebyte (HI); writebyte (LO); writestring (""); writehex (HI); writehex (LO); count ++ ;} else {writegbspace (); writestring ("null");} writestring (""); writebytebuffer (uxbb, 2); writestring (""); writebytebuffer (u8bb, 3 ); if (J % 2 = 0) {writeln () ;}else {writestring ("") ;}} writefooter () ;}} system. out. println ("Number of GB characters wrote:" + count);} public static void writeln () THR Ows ioexception {out. write (0x0d); out. write (0x0a);} public static void writebyte (byte B) throws ioexception {out. write (B & 0xff);} public static void writebytebuffer (bytebuffer B, int L) throws ioexception {int I = 0; If (B = NULL) {writestring ("null"); I = 2;} else {for (I = 0; I <B. limit (); I ++) writehex (B. get (I) ;}for (Int J = I; j <L; j ++) writestring ("") ;} public static void writegbspace () Throws ioexception {out. Write (0xa1); Out. Write (0xa1);} public static void writestring (string s) throws ioexception {If (s! = NULL) {for (INT I = 0; I <S. length (); I ++) {out. write (INT) (S. charat (I) & 0xff) ;}} public static void writenumber (int I) throws ioexception {string S = "00" + String. valueof (I); writestring (S. substring (S. length ()-2, S. length ();} public static void writehex (byte B) throws ioexception {out. write (INT) hexdigit [(B> 4) & 0x0f]); out. write (INT) hexdigit [B & 0x0f]);} public static void writeheader () throws ioexception {writestring ("<PRE>"); writeln (); writestring ("Q. w. "); writegbspace (); writestring (" GB uni. UTF-8 "); writestring (" "); writestring (" Q. w. "); writegbspace (); writestring (" GB uni. UTF-8 "); writeln ();} public static void writefooter () throws ioexception {writestring (" </PRE> "); writeln ();} public static Boolean validgb (int I, Int J) {for (int l = 0; L <B _out.length; l ++) {if (I * 100 + j> = B _out [l] & I * 100 + j <= e_out [l]) return false;} return true ;}}

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.