The understanding and solution of garbled characters in Java

Source: Internet
Author: User

A few days ago in the use of ZTE's MM7 interface to develop an application of MMS, but in the test, the text content of the phone is always a bunch of garbled, no matter how to modify the parameters of the problem is not solved, so hard to ponder and study the content of the code, finally successfully solved the problem of garbled.


We all know that the Java language uses Unicode encoding. But do you really understand the meaning of this statement? What does Unicode encoding have to do with the UTF-8,GBK we use? Then let's discuss the subject.


I wonder if you have ever thought about the code in the JVM when we define a string literal in the Java code. Yes, it also says that the Java language uses Unicode encoding, so the defined string literal is naturally Unicode encoded. However, in addition to the internal data of this program, we often also get a variety of data from the external to the program processing, such as the IO from the hard disk to read files, through the JDBC database, and so on, and the data encoding format is various, for example, some may be UTF-8, some may be GBK, So what code does this data represent in the JVM?


The above mentioned a few questions, now return to the simple to say that the code, of course, here is only a rough mention, after all, the online content of many.


We will first clarify two concepts, "character set Encoding" and "encoding format". The so-called "character set Encoding" is a "character and number mapping relationship table" developed by an organization, for example: 10001 = = "I", 10002 = "we". Our so-called Unicode character set is such a relational table.


UTF-8 is a "coded format" that implements the encoding of some characters in the Unicode character set, which exists for the purpose of saving or transmitting data. So since there is already a mapping in the character set, why do you want to encode it again? There are several reasons for this, such as considering file size issues, usage efficiency issues, and other character set-distinguishing issues.


So what part of the Unicode character set does the UTF-8 code encode?  is the "character number", such as the 10001,10002 above. The code rules for UTF-8 can be accessed online.


Now we should make it clear that in the JVM, the string "we" uses a format such as "1000110002", which is the Unicode number. When we read a UTF-8 encoded text file into memory via IO, it is likely that we will use this code:


New InputStreamReader (new FileInputStream (file), "UTF-8")


The "UTF-8" specified here is the encoded format of the file, which is intended to tell the decoded object to decode the Unicode encoding used by the JVM in the encoded format of "UTF-8". If we are designated as "GBK" here, we can get a bunch of garbled characters.


If string content = (file contents). Then this content is already Unicode encoded at this time. I don't know what I should have understood so far.


If you see a code like this:
String msg = "I am a string";
string res = new String (Msg.getbytes ("Utf-8"), "GBK");

The res here prints out a bunch of garbled characters. The procedure here is actually:
1, the Unicode encoded MSG is encoded in UTF-8 encoding format, resulting in a UTF-8 encoded byte array.
2, the byte array is decoded into Unicode encoded characters according to the encoding format of GBK
3, unfortunately UTF-8 and GBK incompatible, they used a different encoding set, so garbled out


In the HTTP request, we need to tell each other what the encoding format of the data we send, if the other side in accordance with our message encoding format to decode, and we have the wrong encoding format to tell each other, then the next thing is the other party received a bunch of garbled.


And that's when we're doing this: "I'm a string." GetBytes (); Be careful, the byte array you get at this time is the default encoding format for your operating system (and of course we can specify our default encoding format at startup).


The above content about the code in Java about the problem, but also a way to stimulate the process, welcome to correct.




This article comes from the "Simple and Easy" blog, so be sure to keep this source http://dengshuangfu.blog.51cto.com/8794650/1729990

The understanding and solution of garbled characters in Java

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.