zoukankan      html  css  js  c++  java
  • Flex【原创】字符编码操作/字符串二进制互转

    提供一个工具类

    字符编码的操作相关

    直接上源码了:

    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;
            }
            
        }
    }
  • 相关阅读:
    html标签嵌套规则
    关于setTimeout和Promise执行顺序问题
    vue基础
    new操作符具体干了什么
    ["1", "2", "3"].map(parseInt)
    线性表
    树的一些概念和性质
    A*与IDA*
    树上启发式合并
    启发式合并
  • 原文地址:https://www.cnblogs.com/loveFlex/p/2593341.html
Copyright © 2011-2022 走看看