Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Attribute VB_Name = "mime64"
- Option Explicit
- Function Base64Encode(InputStream As String) As String
- Dim Base64Alphabet As String
- Base64Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
- Dim OutputStream As String
- Dim ByteDecVal As Integer
- Dim BitBinVal As Integer
- Dim ByteGroup As String
- Dim ByteGroupBinStream As String
- Dim Padding As String
- Dim BytePtr As Long
- Dim Index As Integer
- Do While InputStream <> ""
- ByteGroup = Left$(InputStream, 3)
- Do While ByteGroup <> ""
- For Index = 7 To 0 Step -1
- ByteDecVal = Asc(Left$(ByteGroup, 1))
- BitBinVal = BIT(ByteDecVal, Index)
- ByteGroupBinStream = ByteGroupBinStream & Trim$(Str$(BitBinVal))
- Next Index
- ByteGroup = Mid$(ByteGroup, 2)
- Loop
- If Len(ByteGroupBinStream) < 24 Then
- Padding = String$((((Len(ByteGroupBinStream) / 8) - 3) * -1), "=")
- ByteGroupBinStream = ByteGroupBinStream & String$(Int(24 / Len(ByteGroupBinStream)) + 1, "0")
- End If
- Do While ByteGroupBinStream <> ""
- BytePtr = Val(BitToDec("00" & Left$(ByteGroupBinStream, 6))) + 1
- OutputStream = OutputStream & Mid$(Base64Alphabet, BytePtr, 1)
- ByteGroupBinStream = Mid$(ByteGroupBinStream, 7)
- Loop
- OutputStream = OutputStream & Padding
- ByteGroupBinStream = ""
- InputStream = Mid$(InputStream, 4)
- Loop
- Base64Encode = OutputStream
- End Function
- Function BIT(ByteDecVal As Integer, Index As Integer) As Integer
- Dim bIndex As Integer
- bIndex = 2 ^ Index
- BIT = IIf((ByteDecVal And bIndex) > 0, 1, 0)
- End Function
- Function BitToDec(BitStr As String) As Long
- Dim Results As Long, CiDx As Long
- For CiDx = 8 To 1 Step -1
- Results = Results + (Val(Mid$(BitStr, CiDx, 1)) * (2 ^ (8 - CiDx)))
- Next CiDx
- BitToDec = Results
- End Function
Advertisement
Add Comment
Please, Sign In to add comment