Importjava.io.UnsupportedEncodingException;/*** Encoding of converted strings*/ Public classChangecharset {/**7-bit ASCII character, also known as the basic Latin block of the iso646-us, Unicode character set*/ Public Static FinalString us_ascii = "Us-ascii"; /**ISO Latin alphabet, also known as Iso-latin-1*/ Public Static FinalString iso_8859_1 = "Iso-8859-1"; /**8-bit UCS conversion format*/ Public Static FinalString utf_8 = "UTF-8"; /**16-bit UCS conversion format, Big Endian (lowest address holds high byte) byte order*/ Public Static FinalString utf_16be = "Utf-16be"; /**16-bit UCS conversion format, Little-endian (highest address holds low byte) byte order*/ Public Static FinalString utf_16le = "Utf-16le"; /**16-bit UCS conversion format, byte order identified by an optional byte order mark*/ Public Static FinalString utf_16 = "UTF-16"; /**Chinese Super large character set*/ Public Static FinalString GBK = "GBK"; /*** Convert character encoding to US-ASCII code*/ PublicString Toascii (String str)throwsunsupportedencodingexception{return This. Changecharset (str, us_ascii);} /*** Convert character encoding to iso-8859-1 code*/ PublicString Toiso_8859_1 (String str)throwsunsupportedencodingexception{return This. Changecharset (str, iso_8859_1);} /*** Convert character encoding to UTF-8 code*/ PublicString Toutf_8 (String str)throwsunsupportedencodingexception{return This. Changecharset (str, utf_8);} /*** Convert character encoding to UTF-16BE code*/ PublicString Toutf_16be (String str)throwsunsupportedencodingexception{return This. Changecharset (str, utf_16be);} /*** Convert character encoding to Utf-16le code*/ PublicString Toutf_16le (String str)throwsunsupportedencodingexception{return This. Changecharset (str, utf_16le);} /*** Convert character encoding to UTF-16 code*/ PublicString toutf_16 (String str)throwsunsupportedencodingexception{return This. Changecharset (str, utf_16);} /*** Convert character encoding to GBK code*/ PublicString TOGBK (String str)throwsunsupportedencodingexception{return This. Changecharset (str, GBK);} /*** Implementation method of string encoding conversion *@paramSTR to convert the encoded string *@paramnewcharset Target Code *@return * @throwsunsupportedencodingexception*/ Publicstring Changecharset (String str, string newcharset)throwsunsupportedencodingexception {if(str! =NULL) { //decodes a string with the default character encoding. byte[] bs =str.getbytes (); //generate a string with a new character encoding return NewString (BS, Newcharset); } return NULL; } /*** Implementation method of string encoding conversion *@paramSTR to convert the encoded string *@paramOldcharset Original Code *@paramnewcharset Target Code *@return * @throwsunsupportedencodingexception*/ Publicstring Changecharset (String str, string oldcharset, string newcharset)throwsunsupportedencodingexception {if(str! =NULL) { //decodes the string with the old character encoding. Decoding may occur with exceptions. byte[] bs =str.getbytes (Oldcharset); //generate a string with a new character encoding return NewString (BS, Newcharset); } return NULL; } Public Static voidMain (string[] args)throwsunsupportedencodingexception {changecharset test=NewChangecharset (); String Str= "This is a Chinese string!"; System.out.println ("STR:" +str); String GBK=TEST.TOGBK (str); System.out.println ("Convert to GBK code:" +GBK); System.out.println (); String ASCII=test.toascii (str); System.out.println ("Convert to Us-ascii code:" +ASCII); GBK=Test.changecharset (Ascii,changecharset.us_ascii, CHANGECHARSET.GBK); System.out.println ("Then convert the ASCII code string to GBK code:" +GBK); System.out.println (); String iso88591=test.toiso_8859_1 (str); System.out.println ("Convert to Iso-8859-1 code:" +iso88591); GBK=Test.changecharset (Iso88591,changecharset.iso_8859_1, CHANGECHARSET.GBK); System.out.println ("Then convert the Iso-8859-1 code string into a GBK code:" +GBK); System.out.println (); String UTF8=test.toutf_8 (str); System.out.println ("Convert to UTF-8 code:" +UTF8); GBK=Test.changecharset (Utf8,changecharset.utf_8, CHANGECHARSET.GBK); System.out.println ("Then convert the UTF-8 code string into a GBK code:" +GBK); System.out.println (); String Utf16be=test.toutf_16be (str); System.out.println ("Convert to Utf-16be code:" +utf16be); GBK=Test.changecharset (Utf16be,changecharset.utf_16be, CHANGECHARSET.GBK); System.out.println ("Then convert the Utf-16be code string into a GBK code:" +GBK); System.out.println (); String Utf16le=Test.toutf_16le (str); System.out.println ("Convert to Utf-16le code:" +utf16le); GBK=Test.changecharset (Utf16le,changecharset.utf_16le, CHANGECHARSET.GBK); System.out.println ("Then convert the Utf-16le code string into a GBK code:" +GBK); System.out.println (); String UTF16=test.toutf_16 (str); System.out.println ("Convert to UTF-16 code:" +UTF16); GBK=Test.changecharset (Utf16,changecharset.utf_16le, CHANGECHARSET.GBK); System.out.println ("Then convert the UTF-16 code string into a GBK code:" +GBK); String s=NewString ("Chinese". GetBytes ("UTF-8"), "UTF-8"); System.out.println (s); }}
Android Learning-Other-string transcoding tool