Judge whether the characters in GBK encoding are garbled

Source: Internet
Author: User

GBK is double-byte encoding, but it is backward compatible with gb2312. That is to say, punctuation marks, letters, and numbers are all marked with one byte.

I need to read an encrypted text file.

There are two restrictions. First, I cannot read the entire text at a time. Because the text is large, the mobile phone cannot afford it. Second, because the file is encrypted to process each byte, I must first read the file with a byte stream and decrypt it to convert it to a character.

When I read a piece of content with throttling for the first time, the last byte may be the first half of a Chinese character. If no processing is performed, it is displayed. The last character is garbled for the first time, and then continues to read and display. A large segment of Garbled text is displayed later. The following figure shows the best description.

"A bunch of exorcism halo falls on the ancient miracle tree, and the death wound is removed. However"

I read a fixed length each time. For example, I read 16 bytes each time. The first read is okay. The second read is because "," only occupies one byte, therefore, the last byte read this time is the high byte of the word "dead". The half byte cannot be correctly encoded, so it is "?" Instead, when I was reading for the third time, the original "B2 F8" character was coded according to "F6 B2", so that the subsequent content would be messy.

 

To solve this problem, I must determine whether the last byte is a high byte. Because if the end is a low byte or a single byte, it will not affect the subsequent operations.

In byte stream, I have not found a method yet, so I can judge it in characters.

After each segment of data is decrypted, it is converted to the new string (_ data, "GBK"). tochararray ();

Then, determine whether the last character is 65533, because if the character is converted into GBK in half a byte, it cannot be converted, so 65533 will be used to replace this unhandled character.

After judgment, save the half byte and place it before the data to be read next time. convert it into a character together.

 

================================ Update ========== ==========================================

The above method can only be implemented on the simulator, not on the real machine, because on the real machine, if there are half a byte at the end, it will be automatically removed, I used n73 for a test, so I'm sure it's my solution. It's okay to test it on a real machine.

 

I need to read a file, but I cannot load it at a time. I can only read it in segments. I have solved this problem.
First, you need to know the GBK encoding range: first byte: 0x81 ~ 0xfe; last byte: 0x40 ~ 0xfe
The reason for garbled characters is that the truncated position is incorrect, resulting in a combination of the last byte and height into a Chinese character.
Therefore, we need to determine whether the first or last byte is a high byte to solve this problem.
According to the GBK encoding range, we can be sure of 2 points.
1:0 <x <0x40 must be a punctuation or other single-byte character
2: x <0x81 | x> 0x40 must be a low byte
If X> 0x81, it may be a high byte or a low byte.
So we can look for the second (X-1) (or the previous (x + 1) byte to determine what it is
If the last (X-1) byte is a punctuation or low byte, X must be a high byte
If the last (X-1) is also a high byte, keep searching

In this way, we can avoid garbled Characters During encoding.

Public Boolean charcompare (byte [] Str) {<br/> Boolean bln = false; <br/> int I = Str. length-1; <br/> Int J = 0; <br/> system. out. println ("file. charcompare () ["+ STR [I] + "]"); <br/> If (STR [I]> 0 & STR [I] <0x40) {<br/> system. out. println ("file. charcompare () [punctuation] ["+ STR [I] + "]"); <br/>} else if (STR [I] <0 & STR [I] <-127) | STR [I]> 64) {<br/> system. out. println ("file. charcompare () [Low byte] ["+ STR [I] + "]"); <br/>} else {<br/> while (STR [I-1] <0 & STR [I-1]>-127) {<br/> system. out. println ("file. charcompare () [forward] ["+ STR [I-1] +"] "); <br/> J ++; <br/> I --; <br/>}< br/> system. out. println ("file. charcompare () [found] ["+ STR [I-1] +"] ["+ J +"] "); <br/> If (J % 2 = 0) <br/> return bln = true; <br/>}< br/> return bln; <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.