Complete java String Encoding and conversion code

Source: Internet
Author: User

Complete java String Encoding and conversion code

Package book. String;

Import java. io. UnsupportedEncodingException;

/***//**
* Encode the conversion string
* @ Author joe
*
*/

Public class ChangeCharset ...{
/*** // *** 7-bit ASCII character, also known as the basic Latin block of the ISO646-US and Unicode character set */
Public static final String US_ASCII = "US-ASCII ";
/*** // *** ISO Latin alphabet No.1, also known as ISO-LATIN-1 */
Public static final String ISO_8859_1 = "ISO-8859-1 ";
/***** // ** 8-bit UCS conversion format */
Public static final String UTF_8 = "UTF-8 ";
/*** // *** The 16-bit UCS conversion format. The Big Endian (the lowest address stores the high byte) byte sequence */
Public static final String UTF_16BE = "UTF-16BE ";
/*** // *** 16-bit UCS conversion format, Litter Endian (highest address storage position byte) byte order */
Public static final String UTF_16LE = "UTF-16LE ";
/*** // ** The 16-bit UCS conversion format. The byte sequence is identified by optional byte sequence tags */
Public static final String UTF_16 = "UTF-16 ";
/***** // ** Chinese character set with a large character set **/
Public static final String GBK = "GBK ";

Public static final String GB2312 = "GB2312 ";

/*** // Convert the character encoding to the US-ASCII code */
Public String toASCII (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, US_ASCII );
}

/*** // Convert character encoding to ISO-8859-1 */
Public String toISO_8859_1 (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, ISO_8859_1 );
}

/*** // Convert character encoding to UTF-8 */
Public String toUTF_8 (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, UTF_8 );
}

/*** // Convert character encoding to UTF-16BE */
Public String toUTF_16BE (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, UTF_16BE );
}

/*** // Convert character encoding to UTF-16LE */
Public String toUTF_16LE (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, UTF_16LE );
}

/*** // Convert character encoding to UTF-16 */
Public String toUTF_16 (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, UTF_16 );
}

/***** // Convert the character encoding to GBK */
Public String toGBK (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, GBK );
}

/***** // Convert the character encoding to GB2312 */
Public String toGB2312 (String str) throws UnsupportedEncodingException ...{
Return this. changeCharset (str, GB2312 );
}

/***//**
* Implementation of string encoding conversion
* @ Param str the string to be converted
* @ Param newCharset destination encoding
*/
Public String changeCharset (String str, String newCharset) throws UnsupportedEncodingException ...{
If (str! = Null )...{
// Use the default character encoding to decode the string. It is related to the system. The default value of windows in Chinese is GB2312.
Byte [] bs = str. getBytes ();
Return new String (bs, newCharset); // Encode a new character to generate a String.
}
Return null;
}

/***//**
* Implementation of string encoding conversion
* @ Param str the string to be converted
* @ Param oldCharset source character set
* @ Param newCharset target character set
*/
Public String changeCharset (String str, String oldCharset, String newCharset) throws UnsupportedEncodingException ...{
If (str! = Null )...{
// Decodes a string using the source character encoding
Byte [] bs = str. getBytes (oldCharset );
Return new String (bs, newCharset );
}
Return null;
}

Public static void main (String [] args) throws UnsupportedEncodingException ...{
ChangeCharset test = new ChangeCharset ();
String str = "This is a Chinese String! ";
System. out. println ("str:" + str );

String gbk = test. toGBK (str );
System. out. println ("converted to GBK code:" + gbk );
System. out. println ();

String ascii = test. toASCII (str );
System. out. println ("convert to US-ASCII:" + ascii );
System. out. println ();

String iso88591 = test. toISO_8859_1 (str );
System. out. println ("converted to ISO-8859-1 code:" + iso88591 );
System. out. println ();

Gbk = test. changeCharset (iso88591, ISO_8859_1, GBK );
System. out. println ("then convert the ISO-8859-1 code string into GBK code:" + gbk );
System. out. println ();

String utf8 = test. toUTF_8 (str );
System. out. println ();
System. out. println ("convert to UTF-8 code:" + utf8 );
String utf16be = test. toUTF_16BE (str );
System. out. println ("convert to UTF-16BE code:" + utf16be );
Gbk = test. changeCharset (utf16be, UTF_16BE, GBK );
System. out. println ("then convert the characters encoded by the UTF-16BE to GBK:" + gbk );
System. out. println ();

String utf16le = test. toUTF_16LE (str );
System. out. println ("convert to UTF-16LE code:" + utf16le );
Gbk = test. changeCharset (utf16le, UTF_16LE, GBK );
System. out. println ("then convert the UTF-16LE-encoded string to GBK:" + gbk );
System. out. println ();

String utf16 = test. toUTF_16 (str );
System. out. println ("convert to UTF-16 code:" + utf16 );
String gb2312 = test. changeCharset (utf16, UTF_16, GB2312 );
System. out. println ("convert the UTF-16-encoded string to GB2312:" + gb2312 );
}

}

The String class in java is encoded according to unicode. When String (byte [] bytes, String encoding) is used to construct a String, encoding refers to the encoding method of the data in bytes, rather than the encoding method of the final String. In other words, enables the system to convert bytes data from encoding to unicode encoding. If this parameter is not specified, jdk determines the bytes encoding method based on the operating system.

When we read data from a file, it is best to use the InputStream method, and then use String (byte [] bytes, String encoding) to specify the file encoding method. Do not use Reader because Reader automatically converts the file content to unicode encoding based on the encoding method specified by jdk.

When we read text data from the database tutorial, we use the ResultSet. getBytes () method to obtain the byte array. We can also use the encoded string construction method.

ResultSet rs;
Bytep [] bytes = rs. getBytes ();
String str = new String (bytes, "gb2312 ");

Do not take the following steps.

ResultSet rs;
String str = rs. getString ();
Str = new String (str. getBytes ("ISO8859-1"), "gb2312 ");

The efficiency of this encoding conversion method is low. The reason for doing so is that when the ResultSet is executed by the getString () method, the data encoding method in the database is ISO8859-1 by default. The system converts the data to unicode according to the encoding method of the ISO8859-1. Use str. getBytes ("ISO8859-1") to restore the data, and then use new String (bytes, "gb2312") to convert the data from gb2312 to unicode, there are many steps in the middle.

When reading parameters from HttpRequest, you can use the reqeust. setCharacterEncoding () method to set the encoding method. The read content is correct.

 


Note:

The default value is utf8. It depends on the encoding of your operating system. I use BIG5 in traditional Windows and utf8 in simplified Windows, the value returned on the page is usually related to the encoding of the jsp tutorial page. Simplified Chinese characters generally use gb2312, while gbk includes gb2312 and big5. The problem here is resolved, the str obtained is not necessarily a gbk, but can be obtained by a gbk str. It filters non-gbk characters. When reading a file, you can add non-gbk characters to the file to see (that is, garbled characters ).

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.