Base64 Encoding
June 3rd, 2007
Often a requirement is given to store binary information, such as a bitmap, inside an XML file or other text file. One way to do this is to encode the file information using Base64 encoding. This code example covers how to encode and decode file data using this method.
First of all, you may ask, “What is Base64 Encoding?” If you want to know all the nuts and bolts of it, check out this Wikipedia article — Base64 — but the easy answer is that it’s a method to encode binary data using only a-z, A-Z, and 0-9 characters.
In the example program, we will take a specified file, read it into memory and encode it as a Base64 string, place it in an XML file, then extract the encoded file from the XML file and write it to disk. First, let’s look at the encoding routine.
Public Function ConvertFileToBase64(ByVal fileName As String) As String
Dim ReturnValue As String = ""
If My.Computer.FileSystem.FileExists(fileName) Then
Using BinaryFile As FileStream = New FileStream(fileName, FileMode.Open)
Dim BinRead As BinaryReader = New BinaryReader(BinaryFile)
Dim BinBytes As Byte() = BinRead.ReadBytes(CInt(BinaryFile.Length))
ReturnValue = Convert.ToBase64String(BinBytes)
BinaryFile.Close()
End Using
End If
Return ReturnValue
End Function
In this routine we check that the specified file exists then open it as a stream. Then we read the data into a BinaryReader object and extract it to a byte array. Note that the number of bytes to read can be specified so that for larger files you could enhance this code to use chunking rather than reading everything in at once. The last step of the process is to convert the byte array to a Base64 string using the built-in Convert class. Also, the Using block insures that the file resource is disposed of properly.
Now we have a string of data we can place in a XML file, a database record, or anywhere else we could place a string. In this example, we will be placing the encoded data string into a XML file as an element.
OK, so now that we have the data in the XML file and saved it to disk, how do we get it back out again? The following code shows how this is done:
Public Function ExtractBase64ToFile(ByVal fileBase As String, ByVal extractFileName As String) As Boolean
Dim FileBytes() As Byte
FileBytes = Convert.FromBase64String(fileBase)
My.Computer.FileSystem.WriteAllBytes(extractFileName, FileBytes, False)
End Function
To reverse the process, we load the string into a byte array and write it to disk. It couldn’t be much simplier. As with the encoding routine, this one could be further enhanced by adding chunking logic and additional exception management.
Using Base64 encoding within VB.NET is an easy way to embed binary data in your portable data files. This applies not only in XML files but other EDI file types, such as HL7 files used in the healthcare field. You can also use it with your local data files to obscure information from the casual viewer. There are many uses for this technique and it’s a useful addition to your VB.NET toolkit.
ยป Click here to download VB.NET source code for this article.
Entry Filed under: Code Examples
Rate This Article:










1 Comment Add your own
1. Chris | April 24th, 2008 at 12:51 pm
Nice example. I noticed that your source download link doesn’t work though…
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed