Base64是一个基于Base64的编码解码程序,它可以把二进制文件和文本文件进行相互转换,用于使仅能以文本方式进行传输和存储的系统能够处理二进制文件,比如在某些只能使用文本方式的Email系统和不能够上传附件的论坛和BBS等上面使用二进制文件。
下面的这段程序可用语处理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
评论