Java character encoding, character transcoding

Source: Internet
Author: User

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

Related Article

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.