/*
Requirement: In Java, the string "ABCD" is the same length as the string "AB Hello", which is four characters.
But the corresponding byte number is different, one Chinese character occupies two bytes.
Defines a method that takes substrings according to the maximum number of bytes.
such as: for "ab Hello", if you take three bytes, then the substring is AB and "you" half of the word,
Then half will be abandoned. If go four bytes is "ab You", take Five bytes or "ab you".
*/
Code: is actually a decoding and coding problems, to understand the difference between UTF-8 and GBK,UTF-8 with three bytes for a Chinese character, GBK use 2 bytes to represent a Chinese character.
And in the code table is the digital storage of these characters. For example, in the GBK code table, "You" is "-60-29"; Good "for"-70-61"," Xie "for"-48-69".
We turn the string into bytes and intercept the string by byte. That is, the number is encoded into a code table, and then the number is translated, that is, the Code table. For GBK, two numbers represent a single character, and the general Chinese character is represented by a negative number. Then we can count the number of negative numbers to determine whether to discard half of the problem of Chinese characters, and then in judging the number of negative numbers of parity, if count is odd, then intercept the last number, discard half of the Chinese characters, decoding output, if it is even, all decoded output.
Public classTest {/** * @paramargs *@throwsIOException*/ Public Static voidMain (string[] args)throwsIOException {String str= "AB Hello cd Thank you";//98-60-29-70-61 100-48-69-48-69 intLen = str.getbytes ("GBK"). length; for(intx=0; x<len; X + +) {System.out.println ("Intercept" + (x+1) + "bytes result is:" +cutstringbybyte (str, x+1)); } //int len = str.getbytes ("Utf-8"). Length; //for (int x=0; x<len; x + +) {//System.out.println ("intercept" + (x+1) + "bytes result is:" +cutstringbyu8byte (str, x+1));// }}/ * String str1 = "coffee";//-84 105
byte[] buf = str1.getbytes ("GBK");
for (byte b:buf) {
System.out.print ("" +b);//-84 105
*/ /*** Use Utf-8 to intercept a string by byte,Utf-8 3 bytes to represent a kanji. */ Public StaticString Cutstringbyu8byte (String str,intLenthrowsIOException {byte[] buf = Str.getbytes ("Utf-8");//CodingSystem.out.println (BUF); intCount = 0; for(intx=len-1; x>=0; x--){ if(buf[x]<0) Count++; Else Break; } if(count%3==0) return NewString (Buf,0,len, "utf-8");//decoding Else if(count%3==1) return NewString (buf,0,len-1, "Utf-8"); Else return NewString (buf,0,len-2, "Utf-8"); } /*** Use byte to intercept string, GBK default one Chinese character is 2 bytes, the encoding table uses two numbers to represent a Chinese character. * (60-29-70-61) Representative Hello * idea: We can record the number of negative numbers, * if the number of digits is even, do not intercept, no half of the case, * if the number of negative numbers is odd, then add 5 numbers, then the last number discarded. becomes 4 digits and then decodes. * @paramSTR *@paramLen *@return * @throwsIOException*/ Public StaticString Cutstringbybyte (String str,intLenthrowsioexception{byte[] buf = Str.getbytes ("GBK");//converts the specified encoded string into bytes, stored in a byte array, encoded intCount = 0; for(intx=len-1; x>=0; x--) {//start looping from the last face of the array, recording the number of negative numbers if(buf[x]<0)//the encoding table for Chinese characters is negativecount++; Else Break; } if(count%2==0)//number of negative numbers is even, do not intercept return NewString (Buf,0,len, "GBK");//decoding Else return NewString (buf,0,len-1, "GBK");//Discard a digit, decode }}