GBK to gb2312

Source: Internet
Author: User

Gb2312 is a subset of GBK. GBK is fully compatible with gb2312. If you want to switch from GBK to gb2312, it is impossible to replace or delete the GBK characters.

Note that there are some blank areas in the GBK symbol area (01-15) and some external hanging Chinese platforms cannot be correctly displayed ~ The blank position in Area 15. Therefore, these blank areas are also removed.

GB encoding rules:
1. character encoding, 682 characters in total:
1) 01 area (94): High A1, low A1-FE
2) 02 area (72): High A2, low B1-E2, E5-EE, F1-FC
3) 03 area (94): High A3, low A1-FE
4) 04 area (83): High A4, low A1-F3
5) 05 area (86): High A5, low A1-F6
6) 06 area (48): High A6, low A1-B8, C1-D8
7) 07 area (66): High A7, low A1-C1, D1-F1
8) 08 area (63): High A8, low A1-BA, C5-E9
9) 09 (76): High A9, low A4-EF
2. Chinese character encoding, 6763 in total:
High B0-F7, low A1-FE, remove the following code:
D7FA-D7FE
A total of 7445 code bits.

 

The conversion code is as follows:

/* <Br/> * 1. character encoding, a total of 682: <br/> 1) 01 (94): High A1, low A1-FE <br/> 2) 02 (72 ): high A2, low B1-E2, E5-EE, F1-FC <br/> 3) 03 (94): High A3, low A1-FE <br/> 4) 04 (83 ): high A4, low A1-F3 <br/> 5) 05 area (86): High A5, low A1-F6 <br/> 6) 06 area (48): High A6, low A1-B8, C1-D8 <br/> 7) 07 area (66): High A7, low A1-C1, D1-F1 <br/> 8) 08 area (63): High A8, low A1-BA, C5-E9 <br/> 9) 09 (76): High A9, low A4-EF <br/> 2. chinese character encoding, a total of 6763: <br/> high B0-F7, low A1-FE, excluding the following encoding: <br/> D7FA-D7FE <br/> */<br/> void isgb2312code (byte [] Str) {<br/> int Hei =-1; // high byte <br/> int low =-1; // low byte <br/> for (INT I = 0; I <Str. length-1; I ++) {<br/> Hei = getint (STR [I]); <br/> low = getint (STR [I + 1]); <br/> // =============================== gb2312 symbol area ============== ================< br/> If (HEI = 0xa1 & (low >=0xa1 & low <= 0xfe )) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("1) 01 area (94): High A1, low A1-FE "); <br/>} else if (HEI = 0xa2 & (low> = 0xb1 & low <= 0xe2) | (low> = 0xe5 & low <= 0xEE) | (low> = 0xf1 & low <= 0xfc) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("2) 02 (72): High A2, low B1-E2, E5-EE, F1-FC "); <br/>} else if (HEI = 0xa3 & (low> = 0xa1 & low <= 0xfe) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("3) 03 (94): High A3, low A1-FE "); <br/>} else if (HEI = 0xa4 & (low> = 0xa1 & low <= 0xf3) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("4) 04 (83): High A4, low A1-F3 "); <br/>} else if (HEI = 0xa5 & (low >=0xa1 & low <= 0xf6) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("5) 04 (86): High A4, low A1-F6 "); <br/>} else if (HEI = 0xa6 & (low> = 0xa1 & low <= 0xb8) | (low> = 0xc1 & low <= 0xd8) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("6) 06 (48): High A6, low A1-B8, C1-D8 "); <br/>} else if (HEI = 0xa7 & (low> = 0xa1 & low <= 0xc1) | (low> = 0xd1 & low <= 0xf1) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("7) 07 (66): High A7, low A1-C1, D1-F1 "); <br/>} else if (HEI = 0xa8 & (low> = 0xa1 & low <= 0xba) | (low> = 0xc5 & low <= 0xe9) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("8) 08 (63): High A8, low A1-BA, C5-E9 "); <br/>} else if (HEI = 0xa9 & (low> = 0xa4 & low <= 0xef) {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("9) 09 (76): High A9, low A4-EF"); <br/>} else if (HEI> = 0xb0 & Hei <= 0xf7) & (low> = 0xa1 & low <= 0xfe) {<br/> If (HEI = 0xd7) & (low> = 0xfa & low <= 0xfe) {<br/> system. out. print (I + ""); <br/> system. out. println ("remove the following encoding: D7FA-D7FE"); <br/>} else {<br/> system. out. print (I + ""); <br/> I ++; <br/> system. out. println ("high B0-F7, low A1-FE "); <br/>}< br/> else {// not gb2312 to Process <br/> If (HEI> = 0x40) // not punctuation to be processed <br/>{< br/> If (HEI <0x81) {// It is a GBK low byte or a character between 64-127 <br/> // If Hei is smaller than 0x81, the Current byte must be a Punctuation Point </P> <p> system. out. println (I + "" + (integer. tohexstring (HEI ). touppercase () + "(@)" + (char) Hei ); <br/>} else {<br/> // * If the Current byte is a high byte of GBK, replace I and I + 1. <br/> // * This may happen I don't know if the expanded ASCII code of 127-255 will be used separately. This is not considered here <br/> // */</P> <p> system. out. println (I + "" + (integer. tohexstring (HEI ). touppercase () + "&" + integer. tohexstring (low ). touppercase (); <br/> STR [I] = (byte) 0xa2; <br/> I ++; <br/> STR [I] = (byte) 0xb2; <br/>}< br/>} else {<br/> system. out. println (I + "" + (integer. tohexstring (HEI ). touppercase () + "(@)" + (char) Hei ); <br/>}< br/>/** convert byte to int */<br/> int getint (byte B) {<br/> int value = B; <br/> If (B <0) <br/> value = 256 + B; <br/> return value; <br/>}

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.