用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧Delphi

Base64

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 20:17:55
快过生蛋节了,俺送大家一段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

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:9,218.75000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号