C# to Ruby how to Base64 Encode

July 9, 2014


Today I needed to Base64 encode a string, so this was a great opportunity to learn how you would do this in Ruby vs. C#


Let’s look at the C# way to do this

using System.Text;

namespace Foo 
	public class Bar 
		public string Base64Encode(string str) 
			var bytes = Encoding.UTF8.GetBytes(str);
            var base64 = Convert.ToBase64String(bytes);
            return base64;

So I need a using to System.Text, I need to get the string as bytes and the I can convert it to a base64 encoded string.

Now how do I do this in Ruby?

require 'base64'

class Foo
   def base64_encode(str)

WAT!?!? I need the a require to base64 and then I can call Base64.encode64, there is not much more to say other than wow. Ruby again is saving me a whole bunch of keystrokes, and I don’t need to write any extension method this is all baked into the framework.


I’m not totally surprised that this was easier in Ruby however I am amazed at how much easier my life is when using Ruby.

Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.

comments powered by Disqus