Java decoding and coding

Source: Internet
Author: User

Next, let's take a look at my code. I have a basic understanding of how to get the unicode, UTF-8, and gbk encoding of strings, and how to get the default character set encoding of the system.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/15225454K-0.jpg "style =" float: none; "title =" vxxd1m_41_smwzhynu1_1_1_qdw.jpg "alt =" 181338987.jpg"/>

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/152254E11-1.jpg "style =" float: none; "title =" B @mongod95k4pvpbtngka1_dqmq8.jpg "alt =" 181342996.jpg"/>


Please see 9th lines of code is how to get the system default Character Set, windows is generally gbk, linux class is Utf-8.


For beginners, the most difficult thing to understand is how many encodings use this encoding and when it is used. This is also my biggest headache at the time.


Next we will first understand what encoding is and what decoding is.

Look at my next code:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1522544531-2.jpg "style =" float: none; "title =" 1f39vh0v5l(0p%7%j5r2o(h.jpg "alt =" 181344600.jpg"/>


There are two lines of key code.

You may still have some questions. Let me raise a few questions to see if you can answer them.


String str = "hello ";

"Hello", how does it exist in the memory?

Unicode. If you do not encode the string in java, it is stored in the memory in Unicode mode.

How to store it?

String is actually a char character array. "Hello" is equivalent to two char arrays of length.

How to store char characters and unicode, so the String object is stored in the memory in Unicode when you do not encode it.

Someone may have questions about this line.

Byte [] buf = str. getBytes ();

He believes that I didn't encode the string here. I just want to get his bytecode array. Why did you say "gbk" to him. Yes. When your OS is windows

Bytep [] buf = str. getBytes ("gbk ");

When you call the string getBytes method, you must encode the string in a character set that is not unicode.

You have not yet understood this point.


String str = new String (buf, "gbk ");

This process is called decoding.

The buf array contains "hello" gbk encoding. Now I want to create a String object. Here I want to point out that your String object is actually a char array. Char stores characters in Unicode format. Therefore, you need to decode the byte code array encoded in other ways into a Unicode string. Of course, the character set you decode must correspond to the character set during encoding; otherwise, it is not a mess.

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.