Simple Base64 Encode/Decode Visual Basic 6
Posted by NitoFeb 1
I just got back in to software development after about a year and a half of focusing purely on web development. I can thank the Raptr client for that, because their newest version of the client is disgusting. It hogs all TCP connections, lagging any online games you may be playing, and slowing web page’s to a crawl. I decided to write my own, with a few additions that I feel would benefit me. In doing this, I *borrowed* Raptr’s games list, adding more games they didn’t have listed. I encoded it in Base64, in case I decide to make it a public application, and thus the hunt for a much simpler Base64 decode function started…
Encoding/Decoding Base64 shouldn’t take hundreds of lines of code to do. I was looking for a simple way to decode a base64 text file, without having to use write up an extensive module. In my journey for a cleaner, more simplistic code, I ran across a nice snippet from Tim Hastings which is probably the simplest way to do this.
First, you’ll need to reference XML in your VB6 project:
Project -> References -> Microsoft XML (Any version => 2.6)

Public Function EncodeBase64(ByRef arrData() As Byte) As String
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
' help from MSXML
Set objXML = New MSXML2.DOMDocument
' byte array to base64
Set objNode = objXML.createElement("b64")
objNode.dataType = "bin.base64"
objNode.nodeTypedValue = arrData
EncodeBase64 = objNode.Text
' thanks, bye
Set objNode = Nothing
Set objXML = Nothing
End Function
Public Function DecodeBase64(ByVal strData As String) As Byte()
Dim objXML As MSXML2.DOMDocument
Dim objNode As MSXML2.IXMLDOMElement
' help from MSXML
Set objXML = New MSXML2.DOMDocument
Set objNode = objXML.createElement("b64")
objNode.dataType = "bin.base64"
objNode.Text = strData
DecodeBase64 = objNode.nodeTypedValue
' thanks, bye
Set objNode = Nothing
Set objXML = Nothing
End Function
Test the above function by using the following code, which decodes a encoded string back in to it’s original text.
Public Sub Main()
Dim strData As String
strData = EncodeBase64(StrConv("ohaithr", vbFromUnicode))
Debug.Print strData
Debug.Print StrConv(DecodeBase64(strData), vbUnicode)
End Sub
The output should be..
b2hhaXRocg==
ohaithr
*Thanks again to Tim Hastings for posting this on his blog.
-Nito



One comment
Comment by Diego Arnaiz (Argentina) on June 28, 2010 at 9:36 am
Thank you! It works great!