概述
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
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: String = Base64. decode(encoded);
07 trace(decoded);
08 //编码和解码ByteArray
09 var obj: Object= {name: "Dynamic Flash",url: "http://dynamicflash.com"};
10 var source: ByteArray = new ByteArray();
11 source. writeObject(obj);
12 var encoded: String = Base64. encodeByteArray(source);
13 trace(encoded);
14 var decoded: ByteArray = Base64. decodeToByteArray(encoded);
15 obj = decoded. readObject();
16 trace(obj. name + "(" + obj. url + ")");
02 //编码和解码字符串
03 var source: String = "Hello, world";
04 var encoded: String = Base64. encode(source);
05 trace(encoded);
06 var decoded: String = Base64. decode(encoded);
07 trace(decoded);
08 //编码和解码ByteArray
09 var obj: Object= {name: "Dynamic Flash",url: "http://dynamicflash.com"};
10 var source: ByteArray = new ByteArray();
11 source. writeObject(obj);
12 var encoded: String = Base64. encodeByteArray(source);
13 trace(encoded);
14 var decoded: 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. public static const version: String = "1.0.0";
6. public static function encode(data: String): String {
7. // Convert string to ByteArray
8. var bytes: ByteArray = new ByteArray();
9. bytes. writeUTFBytes(data);
10. // Return encoded ByteArray
11. return encodeByteArray(bytes);
12. }
13. public static function encodeByteArray(data: ByteArray): String {
14. // Initialise output
15. var output: String = "";
16. // Create data and output buffers
17. var dataBuffer: Array;
18. var outputBuffer: Array = new 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 = new Array();
25. for ( var i: uint = 0; i < 3 && data. bytesAvailable > 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[ 0] & 0x03) << 4) | ((dataBuffer[ 1]) >> 4);
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. for ( 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. for ( var k: uint = 0; k < outputBuffer. length; k++) {
44. output += BASE64_CHARS. charAt(outputBuffer[k]);
45. }
46. }
47. // Return encoded data
48. return output;
49. }
50. public static function decode(data: String): String {
51. // Decode data to ByteArray
52. var bytes: ByteArray = decodeToByteArray(data);
53. // Convert to string and return
54. return bytes. readUTFBytes(bytes. length);
55. }
56. public static function decodeToByteArray(data: String): ByteArray {
57. // Initialise output ByteArray for decoded data
58. var output: ByteArray = new ByteArray();
59. // Create data and output buffers
60. var dataBuffer: Array = new Array( 4);
61. var outputBuffer: Array = new Array( 3);
62. // While there are data bytes left to be processed
63. for ( var i: uint = 0; i < data. length; i += 4) {
64. // Populate data buffer with position of Base64 characters for
65. // next 4 bytes from encoded data
66. for ( var j: uint = 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] = (dataBuffer[ 0] << 2) + ((dataBuffer[ 1] & 0x30) >> 4);
71. outputBuffer[ 1] = ((dataBuffer[ 1] & 0x0f) << 4) + ((dataBuffer[ 2] & 0x3c) >> 2);
72. outputBuffer[ 2] = ((dataBuffer[ 2] & 0x03) << 6) + dataBuffer[ 3];
73. // Add all non-padded bytes in output buffer to decoded data
74. for ( var k: uint = 0; k < outputBuffer. length; k++) {
75. if (dataBuffer[k+ 1] == 64) break;
76. output. writeByte(outputBuffer[k]);
77. }
78. }
79. // Rewind decoded data ByteArray
80. output. position = 0;
81. // Return decoded data
82. return output;
83. }
84. public function Base64() {
85. throw new Error( "Base64 class is static container only");
86. }
87. }
88. }
2. import flash.utils.ByteArray;
3. public class Base64 {
4. private static const BASE64_CHARS: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
5. public static const version: String = "1.0.0";
6. public static function encode(data: String): String {
7. // Convert string to ByteArray
8. var bytes: ByteArray = new ByteArray();
9. bytes. writeUTFBytes(data);
10. // Return encoded ByteArray
11. return encodeByteArray(bytes);
12. }
13. public static function encodeByteArray(data: ByteArray): String {
14. // Initialise output
15. var output: String = "";
16. // Create data and output buffers
17. var dataBuffer: Array;
18. var outputBuffer: Array = new 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 = new Array();
25. for ( var i: uint = 0; i < 3 && data. bytesAvailable > 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[ 0] & 0x03) << 4) | ((dataBuffer[ 1]) >> 4);
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. for ( 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. for ( var k: uint = 0; k < outputBuffer. length; k++) {
44. output += BASE64_CHARS. charAt(outputBuffer[k]);
45. }
46. }
47. // Return encoded data
48. return output;
49. }
50. public static function decode(data: String): String {
51. // Decode data to ByteArray
52. var bytes: ByteArray = decodeToByteArray(data);
53. // Convert to string and return
54. return bytes. readUTFBytes(bytes. length);
55. }
56. public static function decodeToByteArray(data: String): ByteArray {
57. // Initialise output ByteArray for decoded data
58. var output: ByteArray = new ByteArray();
59. // Create data and output buffers
60. var dataBuffer: Array = new Array( 4);
61. var outputBuffer: Array = new Array( 3);
62. // While there are data bytes left to be processed
63. for ( var i: uint = 0; i < data. length; i += 4) {
64. // Populate data buffer with position of Base64 characters for
65. // next 4 bytes from encoded data
66. for ( var j: uint = 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] = (dataBuffer[ 0] << 2) + ((dataBuffer[ 1] & 0x30) >> 4);
71. outputBuffer[ 1] = ((dataBuffer[ 1] & 0x0f) << 4) + ((dataBuffer[ 2] & 0x3c) >> 2);
72. outputBuffer[ 2] = ((dataBuffer[ 2] & 0x03) << 6) + dataBuffer[ 3];
73. // Add all non-padded bytes in output buffer to decoded data
74. for ( var k: uint = 0; k < outputBuffer. length; k++) {
75. if (dataBuffer[k+ 1] == 64) break;
76. output. writeByte(outputBuffer[k]);
77. }
78. }
79. // Rewind decoded data ByteArray
80. output. position = 0;
81. // Return decoded data
82. return output;
83. }
84. public function Base64() {
85. throw new Error( "Base64 class is static container only");
86. }
87. }
88. }
下载类文件
最后
以上就是开放康乃馨为你收集整理的基于 flash AS3.0 的BASE64编码与解码类的全部内容,希望文章能够帮你解决基于 flash AS3.0 的BASE64编码与解码类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复