Code:string->byte[]; Str.getbytes (CharsetName)
Decoding:byte[]->string; New String (Byte[],charsetname);
Package Pack;import Java.io.unsupportedencodingexception;import java.lang.reflect.array;import Java.util.Arrays; Public classdemo{/** * with GBK encoding UTF decoding, and then with UTF encoding, GBK decoding, so can not turn, because GBK and utf-8 inside are Chinese, but Iso8859-1 could*/ Public Static voidMain (string[] args) throws exception{String s="haha"; byte[] B = s.getbytes ("GBK");//GBK EncodingSystem. out. println (arrays.tostring (b)); String Str=NewString (b,"Utf-8");//Utf-8 decodingSystem. out. println (str); b= Str.getbytes ("Utf-8"); System. out. println (arrays.tostring (b)); STR=NewString (b,"GBK"); System. out. println (str); }}
Package Pack;import Java.io.unsupportedencodingexception;import java.lang.reflect.array;import Java.util.Arrays;/** * The unicom with GBK encoding, and then the byte in binary form output, is 110 beginning, followed by 10, so the system is used Utf-8 to decode. * This is a special case, the output binary time, the front has a lot of 1, is 32 bits, the output low 4 bit, on & 255*/ Public classdemo{/** * with GBK encoding UTF decoding, and then with UTF encoding, GBK decoding, so can not turn, because GBK and utf-8 inside are Chinese, but Iso8859-1 could*/ Public Static voidMain (string[] args) throws exception{String s="Unicom"; byte[] B = s.getbytes ("GBK");//GBK Encoding for(byteb1:b) {System. out. println (Integer.tobinarystring (B1)); } System. out. println (arrays.tostring (b)); byte[] bb = S.getbytes ("Utf-8");//GBK EncodingSystem. out. println (arrays.tostring (BB)); for(byteB2:BB) {System. out. println (Integer.tobinarystring (b2&255)); } }}
Java character encoding, character transcoding