Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleEncode and decode text to and from the BinHex and Base64 encodings
DescriptionThis example shows how to encode and decode text to and from the BinHex and Base64 encodings in Visual Basic 6. It uses an XML DOM document to perform the conversions.
Keywordsencode, decode, binhex, hex, base64, base 64
CategoriesMiscellany, Internet
 
Thanks to James Hansen.

The main program displays a string in Base64 encoding and then decodes the encoding to verify that it contains the correct string. It then repeats those steps for the BinHex encoding.

To encode in Base 64, the program makes an XML document and gives it an encode element of type bin.base64. It copies the string into an array of bytes, assigns it to the encoder, and reads the result from its Text property.

To decode the Base 64, the program makes an XML string containing the encoded information, loads it, and reads the decoder node's text.

The functions that encode and decode BinHex work similarly.

 
Private Sub Form_Load()
Dim StringToEncode As String

    StringToEncode = "Hi James!"

    ' To Base64
    Debug.Print Encode(StringToEncode)
    Debug.Print Decode(Encode(StringToEncode))

    ' To Hex
    Debug.Print ToHex(StringToEncode)
    Debug.Print FromHex(ToHex(StringToEncode))
End Sub

Public Function Encode(ByVal iStr As String) As String
Dim iXml As New MSXML2.DOMDocument30
Dim iArray() As Byte

    With iXml.createElement("Encoder")
        .dataType = "bin.base64"
        ReDim iArray(LenB(iStr))
        CopyMemory iArray(0), ByVal StrPtr(iStr), LenB(iStr)

        .nodeTypedValue = iArray()
        Encode = .Text
    End With
End Function

Public Function Decode(ByVal iStrbase64 As String) As Byte()
Dim strXML As String

    strXML = "<DECODER xmlns:dt=" & Chr(34) & _
        "urn:schemas-microsoft-com:datatypes" & Chr(34) & " " & _
        "dt:dt=" & Chr(34) & "bin.base64" & Chr(34) & ">" & _
        iStrbase64 & "</DECODER>"
    With New MSXML2.DOMDocument30
        .loadXML strXML
        Decode = .selectSingleNode("DECODER").nodeTypedValue
    End With
End Function

Public Function ToHex(ByVal iStr As String) As String
Dim iXml As New MSXML2.DOMDocument30
Dim iArray() As Byte

    With iXml.createElement("Encoder")
        .dataType = "bin.hex"
        ReDim iArray(LenB(iStr))
        CopyMemory iArray(0), ByVal StrPtr(iStr), LenB(iStr)

        .nodeTypedValue = iArray()
        ToHex = .Text
    End With
End Function

Public Function FromHex(ByVal iStrbase64 As String) As _
    Byte()
Dim strXML As String

    strXML = "<DECODER xmlns:dt=" & Chr(34) & _
        "urn:schemas-microsoft-com:datatypes" & Chr(34) & " " & _
        "dt:dt=" & Chr(34) & "bin.hex" & Chr(34) & ">" & _
        iStrbase64 & "</DECODER>"
    With New MSXML2.DOMDocument30
        .loadXML strXML
        FromHex = _
            .selectSingleNode("DECODER").nodeTypedValue
    End With
End Function
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated