Short tip: Base64 decoding / encoding class

Stumbled upon Julian’s Base64 encoding / decoding routines, and rewrote it into a class instead, so the code is more self contained. Use it like this:

Dim base64 As New Base64()

Dim encoded As String

encoded = ”QUJDREVGRw==”

print encoded + ” decoded: ” + base64.decode(encoded)

dim decoded as String

decoded = ”ABCDEFG”

Call logger.debug(decoded + ” encoded: ” + base64.encode(decoded))

Call base64.encodeFile(”c:temporiginal.txt”, ”c:tempencoded.txt”)

Call base64.decodeFile(”c:tempencoded.txt”, ”c:tempdecoded.txt”)

A faster, and more beautiful way to check if a String is correctly encoded with Base64, was sent to me by Mats Hasselquist:

 Private Const b64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" Function isValidBase64(inData As String) As Variant isValidBase64 = False If Len(inData) Mod 4 > 0 Then Exit Function If inData Like "*[!" & ENC_BASE64_CHARS & "=]*" Then Exit Function Select Case Instr(inData, "=") Case 1 To Len(inData) - 2 : Exit Function Case Len(inData) - 1 : If Right(inData, 1) <> "=" Then Exit Function End Select isValidBase64=True End Function