Flex [original] character encoding operations/string Binary Conversion

Source: Internet
Author: User
Tags encode string

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 ;}}} 

 

 

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.