Tim Hastings - NonHostile (because there's no need)

Weblog and collection of geeky articles.

  Home :: Who? :: Contact :: Links :: Subscribe subscribe
Privacy Risk With Internet ExplorerMore Industrial Revelations with Mark Williams - God Be Praised!!More Industrial Revelations with Mark Williams - God Be Praised!!


Base64 is a way of representing binary data using alphanumeric characters only, usually used for transmitting binary over a text channel such as email. There are components available to buy for encoding and decoding Base64, but not many people realise it can be done for free without implementing all the gory details.

Rather that implement Base64 Encoding/Decode in Visual Basic the hard way, it is possible to access this functionality provided by Microsoft's XML functions.


This example refers to Visual Basic 6, to do this in VB.Net, check out VB.Net Base64 Encoding in One Line of Code

 

In VB6, add a project reference to Microsoft XML, v2.6 (or later)

 

The following fuctions wrapper the encoding/decoding functionality:

Private 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

 

Private 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

To test the above functions, the following code decodes encoded data, its output should match the input. The StrConv function is used to convert strings into byte arrays and vice versa.

Public Sub Main()

 

    Dim strData As String

 

    strData = EncodeBase64(StrConv("Greetings and Salutations", vbFromUnicode))
    Debug.Print strData 
    Debug.Print
StrConv(DecodeBase64(strData), vbUnicode)

 

End Sub

 

Output...

R3JlZXRpbmdzIGFuZCBTYWx1dGF0aW9ucw==

Greetings and Salutations

Hope this helps.



1211 comments, Visual Basic 6, Monday, April 5, 2004 09:23

Timeline Navigation for Visual Basic 6 posts
VB6: Read XML from a Visual Basic Resource and Return Root Element (made 1 week later)
VB6: Free, Easy and Quick Base64 Encoding and Decoding in Visual Basic (this post, made Monday, April 5, 2004 09:23)
VB6: Read and Write Byte Arrays to Files in Visual Basic (Code Library) (made 22 minutes earlier)


Comments
This is a really smart idea. Thank you so much!

Posted by: Jennifer Rong on Friday, August 26, 2005 15:56
Brillant. Thanks man!
That's what it makes the difference... keeping things simple.

Posted by: Mark Otazu on Friday, November 25, 2005 00:03
hello i am Praveen

Posted by: praveen on Friday, September 7, 2007 10:40
Thanx! Way better than that horrible code we see everywhere when Google-ing for "VB6 Base64"!

Posted by: JP-P on Tuesday, October 16, 2007 16:26
That was great lines of code! I would like to know if is that capable for encoding image file to base64?

Thanks,

Marj

Posted by: marj on Monday, November 26, 2007 11:09
Hello, i am new to VB and dont understand where/what vbUnicode and vbFromUnicode mean or where they come from. Please help :)

strData = EncodeBase64(StrConv("Greetings and Salutations", vbFromUnicode))
Debug.Print strData
Debug.Print StrConv(DecodeBase64(strData), vbUnicode)

Posted by: Chris Rowe on Tuesday, February 5, 2008 06:34
Nice functions!

vbFromUnicode and vbUnicode are constants used by the VbStrConv class. to learn more about these contants, take a look at the StrConv function.

link to msdn StrConv function

http://msdn2.microsoft.com/en-us/library/aa263373.aspx

Posted by: jeffld on Tuesday, February 26, 2008 17:56
Can I use this same function to convert BMP file? if Yes HOW?

Posted by: Premal on Tuesday, April 22, 2008 09:29
How to convert any image file into arrData(). some one suggest me to open image file binary and convert it into arrData() but still it is confusing me. your help will be appriciated

Posted by: Amit kshirsagar on Thursday, June 19, 2008 07:15
How can I use it to convert a file ?

Posted by: JB on Friday, June 20, 2008 15:48
Wow!..This is amazing...thanks a ton friend for your beautiful code which solved my problem...I appreciate your idea....

Posted by: shashi on Friday, August 1, 2008 10:58
Thanks for the code is very hepfull!! It is exactly what I was searching

Posted by: Wiwaty on Wednesday, August 27, 2008 12:48
Thanks. Exactly what I needed. Works great on files.

Posted by: Joel Lebovits on Wednesday, September 10, 2008 04:34
Thanks alot

Posted by: Naderi on Saturday, January 3, 2009 04:42
Elegant and works like a charm. Thanks!

Posted by: John on Friday, March 6, 2009 17:20
The only thing with this code is it has a dependency on msxml2.dll. I would rather have a solution with less dependencies as possible

Posted by: on Monday, March 9, 2009 13:52
Do you have sample code to convert a zipped file to a Base64 encoded file?

Posted by: tremperpg@comcast.net on Tuesday, March 10, 2009 18:59
Being that I have already done this in .NET with one line of code, and the fact that XML is now commonplace, this solution for legacy apps using VBA/VB6 is very much appreciated. Thanks!

Posted by: on Friday, April 10, 2009 23:21
can anyone covert a base64 charset us-ascii email that i received. please email me and i will email you the info,cheers!!!!

Posted by: glenn on Monday, August 17, 2009 22:31
can anyone covert a base64 charset us-ascii email that i received. please email me and i will email you the info,cheers!!! glenn12@supanet.com

ps i will pay you!!

Posted by: glenn on Tuesday, August 18, 2009 22:12
Simple and Awesome!

Posted by: Navision Developer on Thursday, September 17, 2009 06:37
Simple and Awesome!

Posted by: Navision Developer on Thursday, September 17, 2009 06:37
Beautiful! Thanks!

Posted by: Tracy Dryden on Monday, October 26, 2009 22:15
Thanks this is great be blessed man.

Posted by: Nigel on Sunday, November 22, 2009 05:57
Just to thank you for this great code.

Posted by: Doounuts on Monday, January 4, 2010 07:47
This is super! Question... I'm just trying to base encode my image so that I can send it in an xml file. I'm storing everything in a datatable and then using dataset.GETXML(). I assume I need to encode it before I use GETXML(). Is there an easy way to do this? In other words, saving it encoded in the datatable?

Posted by: craig on Wednesday, February 24, 2010 15:57
Awesome! Thanks

Posted by: P-Dat on Wednesday, June 2, 2010 22:13

Post a Comment
Name:  Home page and email address are optional.
  Email addresses will not be displayed or spammed!
Remember these details
Email:
Home Page:
Comment:
Comments cannot contain HTML, URLs will be formatted into hyperlinks.
I reserve the right to remove any comments for any reason.