Skip to content

VBMAN.ToolsBase64 - Base64 Encoding/Decoding Object

Overview

VBMAN.ToolsBase64 provides Base64 encoding and decoding functionality, supporting conversion between strings and byte arrays, commonly used for data transmission, image encoding, configuration files, and other scenarios.

Core Features

  • Bidirectional Conversion: Supports encoding and decoding
  • Multiple Inputs: Supports strings and byte arrays
  • Standard Base64: Compatible with RFC 4648 standard
  • URL Safe: Supports URL-safe Base64 variant

Methods

Encode

Encode string to Base64

vb
Public Function Encode(text As String) As String

Parameters:

  • text - String to encode

Returns: Base64 encoded string

Example:

vb
' Encode string
Dim encoded As String
encoded = VBMAN.ToolsBase64.Encode("Hello World")
Debug.Print encoded  ' SGVsbG8gV29ybGQ=

' Encode Chinese
encoded = VBMAN.ToolsBase64.Encode("Hello World")
Debug.Print encoded  ' 5L2g5aW95LiW55WM

Decode

Decode Base64 to string

vb
Public Function Decode(base64String As String) As String

Parameters:

  • base64String - Base64 encoded string

Returns: Decoded string

Example:

vb
' Decode
Dim decoded As String
decoded = VBMAN.ToolsBase64.Decode("SGVsbG8gV29ybGQ=")
Debug.Print decoded  ' Hello World

BytesToBase64

Convert byte array to Base64

vb
Public Function BytesToBase64(bytes() As Byte) As String

Parameters:

  • bytes - Byte array

Returns: Base64 encoded string

Example:

vb
' Byte array to Base64
Dim bytes(0 To 4) As Byte
bytes(0) = 72: bytes(1) = 101
bytes(2) = 108: bytes(3) = 108
bytes(4) = 111

Dim base64 As String
base64 = VBMAN.ToolsBase64.BytesToBase64(bytes)
Debug.Print base64  ' SGVsbG8=

Base64ToBytes

Convert Base64 to byte array

vb
Public Function Base64ToBytes(base64String As String) As Byte()

Parameters:

  • base64String - Base64 encoded string

Returns: Byte array

Example:

vb
' Base64 to byte array
Dim bytes() As Byte
bytes = VBMAN.ToolsBase64.Base64ToBytes("SGVsbG8=")

' Use byte array
Dim i As Integer
For i = LBound(bytes) To UBound(bytes)
    Debug.Print bytes(i);
Next

Comprehensive Examples

Example 1: Image Base64 Encoding

vb
Private Function ImageToBase64(filePath As String) As String
    ' Read image file
    VBMAN.FileEx.OpenFile filePath, "R"
    Dim bytes() As Byte
    bytes = VBMAN.FileEx.ReadData.ReturnBytes
    VBMAN.FileEx.CloseFile
    
    ' Convert to Base64
    ImageToBase64 = VBMAN.ToolsBase64.BytesToBase64(bytes)
End Function

Private Sub EmbedImage()
    Dim base64Image As String
    base64Image = ImageToBase64("C:\\photo.jpg")
    
    ' Embed in HTML
    Dim html As String
    html = "<img src='data:image/jpeg;base64," & base64Image & "'>"
    WebBrowser1.Document.Write html
End Sub

Example 2: Basic Auth Header

vb
Private Function CreateBasicAuthHeader(username As String, password As String) As String
    ' Combine username and password
    Dim credentials As String
credentials = username & ":" & password
    
    ' Base64 encode
    Dim encoded As String
    encoded = VBMAN.ToolsBase64.Encode(credentials)
    
    ' Build auth header
    CreateBasicAuthHeader = "Basic " & encoded
End Function

Private Sub TestAuth()
    Dim authHeader As String
    authHeader = CreateBasicAuthHeader("admin", "secret123")
    Debug.Print authHeader  ' Basic YWRtaW46c2VjcmV0MTIz
End Sub

Example 3: Config File Encoded Storage

vb
Private Sub SaveEncodedConfig(config As String, filePath As String)
    ' Encode config content
    Dim encoded As String
    encoded = VBMAN.ToolsBase64.Encode(config)
    
    ' Save to file
    VBMAN.FileEx.SetBufferText(encoded, "UTF-8").SaveData filePath
End Sub

Private Function LoadEncodedConfig(filePath As String) As String
    ' Read file
    Dim encoded As String
    encoded = VBMAN.FileEx.OpenFile(filePath, "R").ReadData.ReturnText("UTF-8")
    VBMAN.FileEx.CloseFile
    
    ' Decode
    LoadEncodedConfig = VBMAN.ToolsBase64.Decode(encoded)
End Function

Example 4: Data Transmission

vb
Private Sub SendDataPacket(data As String)
    ' Encode data
    Dim encoded As String
    encoded = VBMAN.ToolsBase64.Encode(data)
    
    ' Build JSON packet
    Dim packet As New cJson
    packet("type") = "data"
    packet("encoding") = "base64"
    packet("content") = encoded
    
    ' Send
    Socket.SendData VBMAN.ToolsUtf8.Encode(packet.Encode)
End Sub

Private Sub ReceiveDataPacket(bytes() As Byte)
    ' Parse JSON
    Dim json As cJson
    Set json = VBMAN.Json.Decode(VBMAN.ToolsUtf8.Decode(bytes))
    
    ' Check encoding
    If json("encoding") = "base64" Then
        ' Decode content
        Dim content As String
        content = VBMAN.ToolsBase64.Decode(json("content"))
        
        ' Process content...
        Debug.Print content
    End If
End Sub

Best Practices

  1. Transmission Encoding: Use Base64 when binary data needs to be transmitted via text protocol
  2. Image Embedding: Use Base64 for embedding small images in HTML/CSS
  3. Auth Info: Use Base64 encoding for HTTP Basic authentication
  4. Data Integrity: After Base64 encoding, data can be safely transmitted in URL, JSON, XML
  5. Size Note: Base64 encoding increases volume by about 33%, consider other solutions for large data

VB6 and LOGO copyright of Microsoft Corporation