dev.kanngard.net make sure you visit my new blog at: johankanngard.net

Short tip: Base64 decoding / encoding class

by Johan Känngård / [LotusScript] / 2003-03-24 / #8


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:\temp\original.txt", "c:\temp\encoded.txt")

Call base64.decodeFile("c:\temp\encoded.txt", "c:\temp\decoded.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 


Floppy icon Base64.lss8 Kb