OpinionatedGeek Logo
Windows Tools

Source code for all these tools is on GitHub.

ADO.NET ConnTest A simple, free Windows program to test ADO.NET connection strings.

Lines of C# Ever wanted to know how many lines of C# code are in a file or folder hierarchy?  This free Windows program will tell you.

XmlTools Free tools to process XML files from the command line.

Base64 encoding and decoding in .NET
What do you do when you get an email asking you a question, but your reply with the answer bounces?
 
I'm not sure what the best answer is, but I figure if I put the answer up here it might help someone else with the same question, even if the original asker doesn't find it.
 
The email I got was about my online base64 encoder and decoder:

Hi

I ask you some help

 

I have big problem of base64 decode

please help me.

 

please base64 decode source in VB.net be given me

Now don't laugh - his English is a hell of a lot better than my Korean.
 
The thing is, base64 encoding and decoding in .NET is utterly trivial - as long as you know the base class library calls to make.  If you don't know they're there, you can spend a while implementing your own algorithm, and that's just Not Fun.
 
So, here's my reply.  Maybe it'll help someone else find the right functions to call.

Hi there,

 

I could send you the file, but I don't think it would help you much.  There's an awful lot of ASP.NET 'plumbing' code and very little base64 code.

 

Instead, here's a sample program I just wrote that shows how to do the base64 encoding and decoding in .NET.  It's in C# - scroll down for the VB.NET version:

 

using System; using System.Text;   public class Base64Decoder {       public static void Main ()       {             string inputText = "This is some text.";             Console.Out.WriteLine ("Input text: {0}", inputText);             byte [] bytesToEncode = Encoding.UTF8.GetBytes (inputText);               string encodedText = Convert.ToBase64String (bytesToEncode);             Console.Out.WriteLine ("Encoded text: {0}", encodedText);               byte [] decodedBytes = Convert.FromBase64String (encodedText);             string decodedText = Encoding.UTF8.GetString (decodedBytes);             Console.Out.WriteLine ("Decoded text: {0}", decodedText);               Console.Out.Write ("Press enter to finish.");             Console.In.ReadLine ();               return;       } }

 

Here's my attempt at translating that to VB.NET.  I'm not a VB.NET programmer, so I may have done something silly.  It seems to work though.

 

imports Microsoft.VisualBasic imports System Imports System.Text   public module Base64Decoder       sub Main             Dim inputText As String             inputText = "This is some text."             Console.Out.WriteLine ("Input text: {0}", inputText)               Dim bytesToEncode As Byte()             bytesToEncode = Encoding.UTF8.GetBytes (inputText)               Dim encodedText As String             encodedText = Convert.ToBase64String (bytesToEncode)             Console.Out.WriteLine ("Encoded text: {0}", encodedText)               Dim decodedBytes As byte()             decodedBytes = Convert.FromBase64String (encodedText)               Dim decodedText As String             decodedText = Encoding.UTF8.GetString (decodedBytes)             Console.Out.WriteLine ("Decoded text: {0}", decodedText)               Console.Out.Write ("Press enter to finish.")             Console.In.ReadLine ()       end sub end module 

 
If you don't want to mess around with byte arrays, you can just use Convert.ToBase64String and Convert.FromBase64String, but doing the conversion to/from a byte array gives you a bit more control if you're faced with strange character encodings.

Hope this helps,

 

                                                Geoff

Unfortunately that reply (to a Korean email address) bounced with the error "PERM_FAILURE: SMTP Error (state 13): 551 User not local". Maybe the person who originally asked the question will find the answer in the blog post.



Categories: .NET
Permalink #.Posted by 'geoff' on Thursday, 06 December 2007 at 8:25AM

Comments, Trackbacks and Pingbacks
Some base64 tips
I just saw a post on Novell's 'Cool Blogs' blog that has a useful tip on base64 decoding.  Apparently, if you save your base64 text to a file with an extension of '.b64', Winzip can open the file and allow you to extract the content.   That...
Posted by 'Geoff's Blog' on Saturday, 12 January 2008 at 2:48PM


Covert a file (pdf, doc etc ) to Base64
Hi
   Please help to do Base64 Encoding and decoding of a file like pdf of doc.
Regards
Pradeep


Posted by 'Pradeep' on Tuesday, 28 September 2010 at 2:37PM


Convert To Japannese
thanks for tutorial. But how to convert japannese character

Posted by 'Zho' on Saturday, 10 November 2012 at 5:29AM


Japanese?

Posted by 'Geoff Taylor' on Sunday, 11 November 2012 at 6:33PM


Password encryption/ decryption code in .NET | ...
Excerpt could not be generated.
Posted by 'Pingback' on Saturday, 16 February 2013 at 2:11PM


Converting files
Pradeep,
 
If you're looking for a quick, simple way to do it, you can go to:

Click 'Choose file' and pick the file you want converted. Then click the 'Encode' button.

The encoded version of the file then appears in the Encoded Output pane. Save all of that text to a file, giving it whatever file name and extension you want).

The blog post you commented on gives more detail on how to write a program to do this, if you're interested.

Hope this helps,

    Geoff

Posted by 'Geoff Taylor' on Sunday, 03 October 2010 at 5:00PM


Thank u
hi this is vetri. really this encoding and decoding information very useful in my project. i've little bit confusion where to implement and how to implement these concept... now i've cleared...

Posted by 'Vetri' on Monday, 15 March 2010 at 3:03PM


HElp me
Hi Please help me i have problem to decode html code in javascript
actually it's encoded at serverside and i want to decode at client side

Posted by 'Ajay' on Thursday, 01 April 2010 at 7:14AM


Javascript encoding/decoding
Hi,
 
A quick search turned up this as a way of doing base64 decoding client-side:

Posted by 'Geoff Taylor' on Thursday, 01 April 2010 at 10:58AM


How do you URL Base64 Encode and Decode a conca...
Excerpt could not be generated.
Posted by 'Pingback' on Tuesday, 09 December 2014 at 9:50PM
Post A Comment
Your Name:
Your URL (optional):
Comment Title:
RSS 2.0 Subscribe to the RSS 2.0 feed for Geoff’s Blog.