日期:2012-12-14  浏览次数:21069 次

快过生蛋节了,俺送大家一段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.