Fuzhaoyang: Conversion of string to byte[in Java
Original address: http://blog.csdn.net/llwan/article/details/7567906
String t = new string (b);//bytep[] converted to string
Do Java often encounter Chinese garbled problem, there are various coding problems, especially the content of the string class need to re-encode the problem. To solve these problems, it is important to understand how Java handles strings.
1. "Character" is represented by a number
First to re-understand how the computer handles the "character", this principle is everyone must remember, especially in the Java writing program, it is absolutely not blurred. We know that computers use numbers to denote everything, and "character" is no exception. For example, we want to display an Arabic numeral "3", in our PC, in fact, is not just a number to represent the "3" we want to write, but to the 16-0x33 to represent, including in memory or write to the file, in fact, are written 0x33, do not believe you can edit a text file, Write a "3" and then use UltraEdit to see his original code.
2. All "Characters" must be represented by a digital + coded table.
At this time, there is a question: why must use 0X33 to represent "3"? Instead of 0x43 to represent it? Or is it a direct substitute for 0x03? In fact, with what to represent all can, but everyone is accustomed to using ASCII encoding table (is the United States National Information exchange table) to determine what the number of characters should be represented. Similarly, in order to express Chinese characters, China has also designated a Chinese encoding table, of which the most widely used is GB2312. For example, the Chinese "when" word, is to use 0xb5, 0xb1 the two eight-digit numbers to express. So if the program that displays the characters doesn't know what coding table the numbers are encoded on, he can't judge exactly what these words are. If you use a wrong coding table to handle these numbers, the characters that are processed are probably completely wrong. For example, in the English system, there is no GB2312 encoding table, give him a 0xb5,0xb1, he foolishly as ASCII to deal with (the operating system usually has its own default encoding table), the results show is two strange symbols, because the two words in the ASCII table is the two symbols. Also in the traditional Chinese system, his coding table is BIG5, showing it is also a strange Chinese, not "when" word.
3. Unicode makes the world speak one language
After reading the above text, do you feel that the world has so many languages, each has its own set of code table, very troublesome? Even in Chinese, there are two sets of popular coding tables, one is GB2312, the other is BIG5. To use different Chinese coded characters, it's really troublesome to go around. Not only this, if you want to write an article containing a lot of over-the-country text, it is troublesome, must let the process of handling this article know, which Word is what coding standards. If you want to find a word in the article, you must also specify which encoding you are looking for. Otherwise, you need to find a 0xb5,0xb1 Chinese "when" word, it is likely to be the same number of Japanese, Polish these unrelated words to you find out, enough trouble!
So people think, as we all use the same coding standard bar, all kinds of text in the coding table has a place, processing the text of the program only need to press the code sheet to deal with it. But to an encoding table contains all the text, this table is large, originally English text + number is only 128 or less. But with the Chinese, suddenly there are tens of thousands of more, so the size of a character needs to be a lot larger. Now Unicode specifies that a character must be represented by 2 8-digit numbers, think, 8x8x8x8x = 65536, is how big a number Ah! So all the words in the world are included. Of course, some people say that Chinese characters may be more than 60,000 pull, but also to include other words, but others think you Chinese people often do not have so much, so decided, we have no way. It is important to note that although both GB2312 and Unicode use two 8-digit numbers to represent a Chinese text, the specific specifications are different, for example, 0XB5,0XB1 in Unicode is not a "when" word, but another country's text.
4. C is how to deal with characters concisely
Let's talk about the C string. C language was born before Java, the basic data type of C language is no string of this type, it only char[]. That is, C put the character order into a byte array is finished. and C does not matter what text is placed in the array, and whether the words are coded according to what standards. And his char size is not necessarily a 8-digit number, sometimes 16-bit is also possible, depending on the specific machine and operating system. So the person who writes the program must know that the content of the char[] that is being processed is exactly the string that is represented by the encoding table, and you know that it doesn't make any sense to compare the two languages.
5. How Java handles characters.
The world will always progress, Java is an example. Java finally has the string class, which is the best tool for solving character problems. In Java, a basic point is: The String class object is not required to specify the encoding table! Why does it know what character a bunch of numbers represent? This is because the character information in string is stored in Unicode encoding. and Java in order to represent the character (note is a single character), there is a char this data type, and his size is fixed 2 8-bit 16 binary number length, that is, 0~65535 ROM. is one of the characters in the corresponding Unicode. If you want to take a string in the Unicode number, you can use GetChars (int srcbegin, int srcend, char[] DST, int dstbegin) method to obtain a char[], this char[] is a number that represents a string character encoded by a Unicode-encoded table.
Unfortunately, most systems and programs do not process characters by Unicode, and Java programs always exchange data with other programs and systems, so when you receive a character or send a character, you have to be aware of the current system and Unicode relationships. For example, you receive a number from the network or file: The 0xb5,0xb1,java program does not know whether the word is Chinese? It's Japanese, or English. If you do not specify the two-digit encoding table, Java will be processed by the current system's default encoding table. If these two numbers are sent from the Chinese WIN98, the Java program is also running on the English Linux, then there is the so-called garbled problem. That is, Java in English encoding table ASCII to deal with these two numbers, when the string obtained through the new string ({0XB5,0XB1}), this string is not the Chinese "as" word, but the two strange characters in English. However, if you know that these two numbers must be in Chinese, you can specify the new string ({0XB5,0XB1}, "GB2312") to handle, when the newly created string is really a "when" word. Of course pull, if you want to put a "when" character of the Java string displayed on the Chinese WIN98, the word must be output to two 8-digit number: 0XB5,0XB1, whether written as a file or output to the browser, must be 0XB5,0XB1. How to use the "when" word with GB2312 output? String.getbytes ("GB2312") can pull! So one thing to remember: exchanging any information with the outside world is done with byte[]. You can look at the majority of Java I/O classes, all with byte[] as parameters and return values. However, there are a lot of confused writing procedures, did not provide byte[] exchange of information methods, harm the different text platform programmers are very headache. This is the Httprequest.getparameter () of the servlet. Fortunately, some jsp/servlet is easy to provide a way to specify the encoding table first, in order to solve this problem relatively simple.
6. Some error handling methods on the internet about Java Chinese.
One is the most common, regardless of what content, use new string (..., "iso-8859-1") to build the string, and then use the time in the default encoding format (usually on the server is the English system) output string. In fact, the string you are using does not use Unicode to represent the real character, but instead forcibly copies the byte array into the char[of string], and once your environment changes, you are forced to modify a lot of code. And it is not possible to process several different coded literals in the same string.
The other is to convert a string of encoded format, such as GB2312, into another format string, such as UTF-8, and then do not specify UTF-8 encoding, but directly with the new string (...). To create a string so that the character placed inside the string is not deterministic, and it represents different characters on different systems. If you ask someone to exchange information using a "UTF-8 format" string, it actually destroys the Java rules for compatibility with various languages. The essence of this mistake is also to write the C language, the string purely as a free-coded memory to use, and ignore the Java string only one encoding format. If you really want to encode freely, use byte[] or char[] to solve the problem completely.
Above, in addition to the basic knowledge of solving the problem of Java Chinese, but also many years ago should master the basic knowledge of the computer. Warm so that the new, with a view to mutual encouragement.
Goto: Conversion of string to byte[in Java