快过生蛋节了,俺送大家一段base64的程序,入口函数是encode(bytfile() as byte),bytfile是一个byte型的数组,返回一个字符串。对了,传入的数组不大于32767个元素(这一点没做什么较验,多了会出错哦!嘻嘻)
———————————————————-
private m_bytindex(0 to 63) as byte
****************************************************
* *
*– to encode file data with base64 method. *
* *
****************************************************
public function encode(bytfile() as byte) as string
dim i as long, j as long
dim strrslt as string
i = 0
for i = 0 to ubound(bytfile) – ((ubound(bytfile) + 1) mod 3) step 3
strrslt = strrslt + chr(m_bytindex(int((bytfile(i) and 252) / 4)))
strrslt = strrslt + chr(m_bytindex(int((bytfile(i) and 3) * 16 + (bytfile(i + 1) and 240) / 16)))
strrslt = strrslt + chr(m_bytindex(int((bytfile(i + 1) and 15) * 4 + (bytfile(i + 2) and 192) / 64)))
strrslt = strrslt + chr(m_bytindex(int(bytfile(i + 2) and 63)))
next i
select case ((ubound(bytfile) + 1) mod 3)
case 1
strrslt = strrslt + chr(m_bytindex(int((bytfile(ubound(bytfile)) and 252) / 4)))
strrslt = strrslt + chr(m_bytindex(int((bytfile(ubound(bytfile)) and 3) * 16)))
strrslt = strrslt + "=="
case 2
strrslt = strrslt + chr(m_bytindex(int((bytfile(ubound(bytfile) – 1) and 252) / 4)))
strrslt = strrslt + chr(m_bytindex(int((bytfile(ubound(bytfile) – 1) and 3) * 16 + (bytfile(ubound(bytfile)) and 240) / 16)))
strrslt = strrslt + chr(m_bytindex(int((bytfile(ubound(bytfile)) and 15) * 4)))
strrslt = strrslt + "="
end select
encode = strrslt
end function
****************************************************
* *
*– class initialize to initialize the array of *
* base64 coding. *
* *
****************************************************
private sub class_initialize()
m_bytindex(0) = 65 asc("a")
m_bytindex(1) = 66 asc("b")
m_bytindex(2) = 67 asc("c")
m_bytindex(3) = 68 asc("d")
m_bytindex(4) = 69 asc("e")
m_bytindex(5) = 70 asc("f")
m_bytindex(6) = 71 asc("g")
m_bytindex(7) = 72 asc("h")
m_bytindex(8) = 73 asc("i")
m_bytindex(9) = 74 asc("j")
m_bytindex(10) = 75 asc("k")
m_bytindex(11) = 76 asc("l")
m_bytindex(12) = 77 asc("m")
m_bytindex(13) = 78 asc("n")
m_bytindex(14) = 79 asc("o")
m_bytindex(15) = 80 asc("p")
m_bytindex(16) = 81 asc("q")
m_bytindex(17) = 82 asc("r")
m_bytindex(18) = 83 asc("s")
m_bytindex(19) = 84 asc("t")
m_bytindex(20) = 85 asc("u")
m_bytindex(21) = 86 asc("v")
m_bytindex(22) = 87 asc("w")
m_bytindex(23) = 88 asc("x")
m_bytindex(24) = 89 asc("y")
m_bytindex(25) = 90 asc("z")
m_bytindex(26) = 97 asc("a")
m_bytindex(27) = 98 asc("b")
m_bytindex(28) = 99 asc("c")
m_bytindex(29) = 100 asc("d")
m_bytindex(30) = 101 asc("e")
m_bytindex(31) = 102 asc("f")
m_bytindex(32) = 103 asc("g")
m_bytindex(33) = 104 asc("h")
m_bytindex(34) = 105 asc("i")
m_bytindex(35) = 106 asc("j")
m_bytindex(36) = 107 asc("k")
m_bytindex(37) = 108 asc("l")
m_bytindex(38) = 109 asc("m")
m_bytindex(39) = 110 asc("n")
m_bytindex(40) = 111 asc("o")
m_bytindex(41) = 112 asc("p")
m_bytindex(42) = 113 asc("q")
m_bytindex(43) = 114 asc("r")
m_bytindex(44) = 115 asc("s")
m_bytindex(45) = 116 asc("t")
m_bytindex(46) = 117 asc("u")
m_bytindex(47) = 118 asc("v")
m_bytindex(48) = 119 asc("w")
m_bytindex(49) = 120 asc("x")
m_bytindex(50) = 121 asc("y")
m_bytindex(51) = 122 asc("z")
m_bytindex(52) = 48 asc("0")
m_bytindex(53) = 49 asc("1")
m_bytindex(54) = 50 asc("2")
m_bytindex(55) = 51 asc("3")
m_bytindex(56) = 52 asc("4")
m_bytindex(57) = 53 asc("5")
m_bytindex(58) = 54 asc("6")
m_bytindex(59) = 55 asc("7")
m_bytindex(60) = 56 asc("8")
m_bytindex(61) = 57 asc("9")
m_bytindex(62) = 43 asc("+")
m_bytindex(63) = 47 asc("/")
end sub
