基于 flash AS3.0 的BASE64编码与解码类
发布时间:2020-12-15 17:29:32 所属栏目:百科 来源:网络整理
导读:版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://programmer.blogbus.com/logs/36436446.html 在国外网站上淘到一个很好用的AS3的BASE64类感觉写得简洁而强大。很好,赶快放上来。 调用代码: 01 ? import ?com.dynamicflash.util
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://programmer.blogbus.com/logs/36436446.html 在国外网站上淘到一个很好用的AS3的BASE64类感觉写得简洁而强大。很好,赶快放上来。 调用代码:
01?
import?com.dynamicflash.utils.Base64;?????
02? //编码和解码字符串?? 03? var?source: String?=? "Hello,world";????? 04? var?encoded: String?= Base64. encode(source);????? 05?trace(encoded);????? 06? var?decoded: decode(encoded);????? 07?trace(decoded);???? 08? //编码和解码ByteArray?? 09? var?obj: Object= {name: "Dynamic Flash",url: "http://dynamicflash.com"};?? 10? ByteArray?=? new? ByteArray();?? 11?source. writeObject(obj);?? 12? encodeByteArray(source);?? 13?trace(encoded);?? 14? ByteArray?= Base64. decodeToByteArray(encoded);?? 15?obj = decoded. readObject();?? 16?trace(obj. name?+? "("?+ obj. url?+? ")"); Base64类代码:
ActionScript 3语言:?
临时自用代码@代码发芽网
1.?
package?com.dynamicflash.util{??
2.? import?flash.utils.ByteArray;?? 3.? public? class?Base64 {?? 4.? private? static? const?BASE64_CHARS: String?= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";?? 5.? const?version: "1.0.0";?? 6.? function?encode(data: String): String?{?? 7.? // Convert string to ByteArray 8.? var?bytes: 9.???????????? bytes. writeUTFBytes(data);?? 10.? // Return encoded ByteArray 11.? return?encodeByteArray(bytes);?? 12.???????? }?? 13.? function?encodeByteArray(data: ByteArray): 14.? // Initialise output 15.? var?output: "";?? 16.? // Create data and output buffers 17.? var?dataBuffer: Array;?? 18.? var?outputBuffer: Array?=? Array( 4);?? 19.? // Rewind ByteArray 20.???????????? data. position?=? 0;?? 21.? // while there are still bytes to be processed 22.? while?(data. bytesAvailable?>? 0) {?? 23.? // Create new data buffer and populate next 3 bytes from data 24.???????????????? dataBuffer =? Array();?? 25.? for?( var?i: uint?=? 0; i <? 3?&& data. 0; i++) {?? 26.???????????????????? dataBuffer[i] = data. readUnsignedByte();?? 27.???????????????? }?? 28.? // Convert to data buffer Base64 character positions and? 29.? // store in output buffer 30.???????????????? outputBuffer[ 0] = (dataBuffer[ 0] &? 0xfc) >>? 2;?? 31.???????????????? outputBuffer[ 1] = ((dataBuffer[ 0x03) <<? 4) | ((dataBuffer[ 1]) >>? 32.???????????????? outputBuffer[ 2] = ((dataBuffer[ 1] &? 0x0f) <<? 2) | ((dataBuffer[ 2]) >>? 6);?? 33.???????????????? outputBuffer[ 3] = dataBuffer[ 2] &? 0x3f;?? 34.? // If data buffer was short (i.e not 3 characters) then set 35.? // end character indexes in data buffer to index of '=' symbol. 36.? // This is necessary because Base64 data is always a multiple of 37.? // 4 bytes and is basses with '=' symbols. 38.? var?j: uint?= dataBuffer. length; j <? 3; j++) {?? 39.???????????????????? outputBuffer[j +? 1] =? 64;?? 40.???????????????? }?? 41.? // Loop through output buffer and add Base64 characters to? 42.? // encoded data string for each character. 43.? var?k: 0; k < outputBuffer. length; k++) {?? 44.???????????????????? output += BASE64_CHARS. charAt(outputBuffer[k]);?? 45.???????????????? }?? 46.???????????? }?? 47.? // Return encoded data 48.? return?output;?? 49.???????? }?? 50.? function?decode(data: 51.? // Decode data to ByteArray 52.? ByteArray?= decodeToByteArray(data);?? 53.? // Convert to string and return 54.? return?bytes. readUTFBytes(bytes. length);?? 55.???????? }?? 56.? function?decodeToByteArray(data: ByteArray?{?? 57.? // Initialise output ByteArray for decoded data 58.? 59.? 60.? 61.? 3);?? 62.? // While there are data bytes left to be processed 63.? 0; i < data. length; i +=? 4) {?? 64.? // Populate data buffer with position of Base64 characters for 65.? // next 4 bytes from encoded data 66.? 0; j <? 4?&& i + j < data. length; j++) {?? 67.???????????????????? dataBuffer[j] = BASE64_CHARS. indexOf(data. charAt(i + j));?? 68.???????????????? }?? 69.? // Decode data buffer back into bytes 70.???????????????? outputBuffer[ 0] <<? 2) + ((dataBuffer[ 0x30) >>? 71.???????????????? outputBuffer[ 4) + ((dataBuffer[ 0x3c) >>? 2);?????????? 72.???????????????? outputBuffer[ 6) + dataBuffer[ 3];?? 73.? // Add all non-padded bytes in output buffer to decoded data 74.? 75.? if?(dataBuffer[k+ 1] ==? 64)? break;?? 76.???????????????????? output. writeByte(outputBuffer[k]);?? 77.???????????????? }?? 78.???????????? }?? 79.? // Rewind decoded data ByteArray 80.???????????? output. 81.? // Return decoded data 82.? 83.???????? }?? 84.? function?Base64() ?{? 85.? throw? Error( "Base64 class is static container only");?? 86.???????? }?? 87.???? }?? 88. } ? 下载类文件 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |