Provides a tool class
Character encoding operations
Directly go to the source code:
Package { Import Flash. utils. bytearray; /** * Encode util * */ Public Class Encodeutil { /** * Encode UTF-8 * @ Param Key ** @ Return String * */ Public Static Function encodeutf8 (key: string): String {var oribytearr: bytearray = New Bytearray (); oribytearr. writeutfbytes (key); var tempbytearr: bytearray = New Bytearray (); For (Var I:Int = 0; I <oribytearr. length; I ++ ){ If (Oribytearr [I] = 194 ) {Tempbytearr. writebyte (oribytearr [I + 1 ]); I ++ ;} Else If (Oribytearr [I] = 195 ) {Tempbytearr. writebyte (oribytearr [I + 1] + 64 ); I ++;} Else {Tempbytearr. writebyte (oribytearr [I]);} tempbytearr. Position = 0 ; Convertstringtobytearray (key ); Return Tempbytearr. readmultibyte (tempbytearr. bytesavailable, "Chinese" ); // Return tempbytearr. readmultibyte (tempbytearr. bytesavailable, "UTF-8 "); } /** * Decode string * @ Param Key * @ Param Charset (default = "UTF-8 ")** @ Return String * */ Public Static Function decodestring (key: String, charset: String = "UTF-8" ): String {var result: String = "" ; Var Byte : Bytearray =New Bytearray (); Byte . Writemultibyte (Key, charset ); Byte . Position = 0 ; For (Var I: Int ; I < Byte . Length; I ++ ){ // Result + = escape (string. fromcharcode (byte [I]); Result + = string. fromcharcode (Byte [I]);} Return Result ;} /** * Encode string * @ Param Key * @ Param Charset (default = "UTF-8 ")** @ Return String * */ Public Static Function encodestring (key: String, charset: String = "UTF-8" ): String {var result: String = "" ; Var Byte : Bytearray = Convertstringtobytearray (key ); Byte . Readmultibyte ( Byte . Bytesavailable, charset ); Byte . Position = 0 ; For (Var I: Int ; I <Byte . Length; I ++ ) {Result + = Escape (string. fromcharcode ( Byte [I]);} Return Result ;} /** * Convert string to bytearray * @ Param Key ** @ Return Bytearray * */ Public Static Function convertstringtobytearray (key: string): bytearray {var Bytes: bytearray; If (Key) {bytes = New Bytearray (); // Writeutfbytes (value: string) Let UTF-8 string write into byte stream; Bytes. writeutfbytes (key );} Return Bytes ;} /** * Convert bytearray to string * @ Param Key ** @ Return String * */ Public Static Function convertbytearraytostring (Bytes: bytearray): String {var STR: string; If (Bytes ){ // When bytearray convert string, please attention with Bytes's position must be 0 Bytes. Position = 0 ; // Readutfbytes (Length: uint): Read byte sequence at length from byte stream, and return a string; STR = Bytes. readutfbytes (bytes. Length );} Return STR ;}}}