flex_String 和 ByteArray 如何進行互相轉換;

來源:互聯網
上載者:User

特別說明:以下字串和 ByteArray 的轉換中, 主要針對字串是 UTF-8 編碼的;

    將 String 轉換成 ByteArray:
        private function convertStringToByteArray(string:String):ByteArray
        {
            var bytes:ByteArray;
            if ( string) {
                bytes = new ByteArray();
                bytes.writeUTFBytes(string);// writeUTFBytes(value:String) 將 UTF-8 字串寫入位元組流;
            }
            return bytes;
        }

     將 ByteArray 轉換成 String:

      private function convertByteArrayToString(bytes:ByteArray):String
        {
            var str:String;
            if ( bytes ) {
                bytes.position = 0; // 在將 ByteArray 轉換成 String中應注意將 bytes 的 position 設定為 0,切記;
                str = bytes.readUTFBytes(bytes.length); // readUTFBytes(length:uint):從位元組流中讀取一個由
length 參數指定的 UTF-8 位元組序列,並返回一個字串;
            }
            return str;
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.