| Schmidt 2004-05-30, 6:30 am |
|
"shachar" <shimshon.shachar@bankleumi.co.il> schrieb im Newsbeitrag
news:E896A532-D081-4636-9C61-4A1EFD94950E@microsoft.com...
> hi all.
> i have a text file written in codebase 64.
> i need an example of how to decode it.
> thanks - shachar.
Here comes a base64-pair:
Public Function Base64Encode$(S$)
Dim B() As Byte, Out() As Byte, i&, j&, L&, Enc() As Byte
If Len(S) = 0 Then Exit Function
Enc = StrConv(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn
opqrstuvwxyz" _
& "0123456789+/", vbFromUnicode)
L = Len(S): B = StrConv(S, vbFromUnicode)
ReDim Preserve B(0 To (UBound(B) \ 3) * 3 + 2)
ReDim Preserve Out(0 To (UBound(B) \ 3) * 4 + 3)
For i = 0 To UBound(B) - 1 Step 3
Out(j) = Enc(B(i) \ 4): j = j + 1
Out(j) = Enc((B(i + 1) \ 16) Or (B(i) And 3) * 16): j = j + 1
Out(j) = Enc((B(i + 2) \ 64) Or (B(i + 1) And 15) * 4): j = j + 1
Out(j) = Enc(B(i + 2) And 63): j = j + 1
Next i
For i = 1 To i - L: Out(UBound(Out) - i + 1) = 61: Next i
Base64Encode = StrConv(Out, vbUnicode)
End Function
Public Function Base64Decode$(S$)
Dim B() As Byte, Out() As Byte,i&,j&,L&,Enc() As Byte,Dec(255) As Byte
If Len(S) = 0 Then Exit Function
Enc = StrConv(" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmn
opqrstuvwxyz" _
& "0123456789+/", vbFromUnicode)
For i = 0 To 255: Dec(i) = 64: Next
For i = 0 To 63: Dec(Enc(i)) = i: Next
L = Len(S): B = StrConv(S, vbFromUnicode)
ReDim Preserve Out(0 To (L \ 4) * 3 - 1)
For i = 0 To UBound(B) Step 4
Out(j) = (Dec(B(i)) * 4) Or (Dec(B(i + 1)) \ 16): j = j + 1
Out(j) = (Dec(B(i + 1)) And 15) * 16 Or (Dec(B(i + 2)) \ 4): j = j + 1
Out(j) = (Dec(B(i + 2)) And 3) * 64 Or Dec(B(i + 3)): j = j + 1
Next i
If B(L - 2) = 61 Then j = 2 Else If B(L - 1) = 61 Then j = 1 Else j = 0
ReDim Preserve Out(0 To UBound(Out) - j)
Base64Decode = StrConv(Out, vbUnicode)
End Function
Olaf
|