String. getBytes ()

Source: Internet
Author: User

In Java, the getBytes () method of String is to get a byte array of the default encoding format of the operating system. This indicates that the returned items are different under the OS!


The String. getBytes (String decode) method returns the byte array representation of a String based on the specified decode encoding, as shown in figure

Byte [] B _gbk = "medium". getBytes ("GBK ");
Byte [] B _utf8 = "medium". getBytes ("UTF-8 ");
Byte [] B _iso88591 = "medium". getBytes ("ISO8859-1 ");


Return "medium" in GBK, UTF-8 and ISO8859-1 encoding of the byte array, the length of B _gbk is 2, B _utf8 is 3, B _iso88591 length is 1.

In contrast to getBytes, you can use the new String (byte [], decode) method to restore the new String (byte [], decode) the encoding specified by decode is used to parse byte [] into a string.

String s_gbk = new String (B _gbk, "GBK ");
String s_utf8 = new String (B _utf8, "UTF-8 ");
String s_iso88591 = new String (B _iso88591, "ISO8859-1 ");

By Printing s_gbk, s_utf8 and s_iso88591, we will find that s_gbk and s_utf8 are both "medium", and only s_iso88591 is an unknown character, why use ISO8859-1 encoding after combination, can not restore the word "in", in fact, the reason is very simple, because the ISO8859-1 encoding table, there is no Chinese characters, of course, can not pass "in ". getBytes ("ISO8859-1"); to get the correct "medium" word in the ISO8859-1 of the encoding value, so through the new String () to restore it will be impossible to talk about.

Therefore. when getBytes (String decode) method is used to obtain byte [], you must make sure that the encoding table of decode does have the code value represented by String, so that the obtained byte [] array can be restored correctly.

Sometimes, in order to adapt Chinese characters to certain special requirements (for example, the http header requires their content to be iso8859-1-encoded), it may be possible to encode Chinese Characters in bytes, such

String s_iso88591 = new String ("medium". getBytes ("UTF-8"), "ISO8859-1 "),

The resulting s_iso8859-1 string is actually three characters in the ISO8859-1, after passing these characters to the destination,

The destination program then gets the correct Chinese character "in" through the opposite way String s_utf8 = new String (s_iso88591.getBytes ("ISO8859-1"), "UTF-8 ". In this way, both compliance with the Agreement and Chinese characters are supported.


========================================================== ==============

Detailed process of JAVA coding and conversion

Common JAVA programs include:
* Classes that run directly on the console (including visual interface classes)
* JSP code class (Note: JSP is a variant of the Servlets class)
* Servelets class
* EJB class
* Other support classes that cannot be directly run
These class files may contain Chinese strings, and we often use the first three types of JAVA programs to directly interact with users for output and input characters, such: we get the characters sent from the client in JSP and Servlet, which also contain Chinese characters. Regardless of the role of these JAVA classes, the lifecycle of these JAVA programs is as follows:
* The programmer selects an appropriate editing software on a certain operating system to implement the source code and. the java extension is stored in the operating system. For example, you can use NotePad to edit a java source program in win2k;
* Programmers use javac.exe in JDK to compile the source code to form a. class (JSP files are compiled by the container by calling JDK );
* Directly run these classes or deploy these classes to WEB containers for running and output the results.
In these processes, how does JDK and JVM encode, decode, and run these files?
Here, we use the Chinese win2k operating system as an example to illustrate how JAVA classes are encoded and decoded.
Step 1: compile a Java source program file (including the above five types of JAVA programs) with editing software such as notepad in win2k ), by default, program files are saved in the GBK encoding format supported by the operating system (the default format supported by the operating system is file. encoding format) to form. java files, that is, before the java program is compiled, our JAVA source program files use the default file supported by the operating system. the encoding format is saved. The java source program contains Chinese characters and English program code. You need to view the system file. you can use the following code to encoding the parameter:
Public class ShowSystemDefaultEncoding {
Public static void main (String [] args ){
String encoding = System. getProperty ("file. encoding ");
System. out. println (encoding );
}}
Compile first obtains the default encoding format used by the operating system, that is, when compiling a java program, if we do not specify the encoding format of the source program file, JDK first obtains the file of the operating system. the encoding parameter (which stores the default encoding format of the operating system, such as WIN2k, whose value is GBK). Then, JDK extracts our java source program from file. the encoding format is converted to the JAVA internal default UNICODE format and placed into the memory. Then, javac compiles the converted unicode format file. class file. the class file is UNICODE encoded and is temporarily stored in the memory. Then, JDK saves the compiled class file encoded with UNICODE to our operating system to form what we see. class file. For us, what we finally get. A class file is a class file whose content is saved in UNICODE encoding format. It contains a Chinese character string in our source program, but it has been written by file. the encoding format is converted to the UNICODE format. In this step, the JSP source code files are different. For JSP, the process is as follows: that is, the WEB Container calls the JSP compiler, the JSP compiler first checks whether the JSP file has a file encoding format. If the JSP file does not have a JSP file encoding format set, the JSP compiler calls JDK to use the default JVM character encoding format (that is, the default file of the operating system where the WEB container is located) for the JSP file. encoding) is converted to a temporary Servlet class, then compiled into a class in UNICODE format, and saved in a temporary folder. For example, in the Chinese win2k, the WEB Container converts the JSP file from the GBK encoding format to the UNICODE format and then compiles it into a temporarily saved Servlet class to respond to user requests.
Step 3: run the classes compiled in step 2:
A. classes run directly on the console
B. EJB class and support class that cannot be directly run (such as JavaBean class)
C. JSP code and Servlet class
D. Between JAVA programs and databases
Let's look at these four situations.
A. classes run directly on the console
In this case, JVM is required to run this class, that is, JRE must be installed in the operating system. The running process is as follows: first, start JVM in java. At this time, JVM reads the class file stored in the operating system and reads the content into the memory. At this time, the class in UNICODE format is used in the memory, then the JVM runs it. If this class needs to receive user input at this time, the class uses file by default. the encoding format encodes the string you entered and converts it to unicode and saves it To the memory (you can set the encoding format of the input stream ). After the program runs, the generated string (UNICODE encoded) is handed back to JVM, and then the JRE converts the string to file. the encoding format (you can set the encoding format of the output stream) is passed to the operating system display interface and output to the interface. The conversion of each step above requires correct encoding format conversion to avoid garbled characters.
B. EJB class and support class that cannot be directly run (such as JavaBean class)
Because EJB classes and support classes that cannot be directly run, they generally do not directly interact with users for input and output. They often interact with other classes for input and output, therefore, after the second step is compiled, the classes whose content is UNICODE encoded are saved in the operating system, in the future, as long as its interaction with other classes is not lost during parameter transmission, it will run correctly.
C. JSP code and Servlet class
After step 2, the JSP file is also converted to a Servlets file, but it does not exist in the classes directory like the standard Servlets one, it exists in the temporary directory of the WEB container, in this step, we also use it as the Servlets.
For Servlets, when the client requests it, the WEB Container calls its JVM to run the Servlet. First, the JVM reads the Servlet class from the system and loads it into the memory, the Servlet class code in the memory is UNICODE encoded, and then the JVM runs the Servlet class in the memory. If the Servlet is running, it needs to accept characters sent from the client, such: the value entered in the form and the value entered in the URL. If no encoding format is set in the program, the WEB Container uses the ISO-8859-1 encoding format by default to accept incoming values and relay to UNICODE format in the memory of the WEB Container in JVM. After the Servlet runs, the output string is in UNICODE format. Then, the container runs the UNICODE string generated by the Servlet (such as html syntax and user output string) it is directly sent to the client browser and output to the user. If the encoding format specified for sending is specified, it is output to the browser according to the specified encoding format. If not specified, by default, it is sent to the client's browser in ISO-8859-1 encoding.
D. Between Java programs and databases
For almost all the JDBC drivers of the database, the default transfer data between the JAVA program and the database is in the ISO-8859-1 as the default encoding format, so, when our program stores data containing Chinese characters to the database, JDBC first converts the data in the UNICODE encoding format inside the program to the ISO-8859-1 format, and then passes it to the database, when the database saves the data, it is saved by ISO-8859-1 by default, so this is why the Chinese data we often read in the database is garbled.

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.