Learn how to use Base64 in programming languages: C#, C++, C, Java
, ,

Base64 Encoding & Decoding in Ruby: Guide for Beginners

An introduction to Base64 encoding and decoding in the Ruby programming language is given in this article. The article describes the fundamentals of Base64, how it operates, and how to use it in Ruby. It is meant for beginners. To assist users comprehend the ideas and get started using Base64 in their own applications, it offers examples and code samples. Anyone interested in learning more about Base64 and its uses in Ruby should check out this excellent resource.

What is Base64?

Base64 is a technique for converting binary data into a human-readable format that may be easily transferred between systems. To maintain interoperability with text-based platforms, byte sequences are encoded into a set of ASCII characters. Although not suitable for encryption, Base64 encoding is beneficial for inserting graphics into emails or embedding files within code since it simplifies the handling of binary data in a variety of applications.

What is Ruby?

Ruby is a dynamic, object-oriented programming language that is well-known for its elegant and understandable syntax. It’s frequently linked to the Ruby on Rails framework, which allows for the rapid construction of web applications. Ruby prioritizes developer productivity and convention over configuration. Because of its emphasis on simplicity and expressiveness, it is a popular choice for web development and scripting activities.

Base64 Encoding & Decoding in Ruby

Ruby’s Base64 module can be used to decode Base64-encoded strings back to their original binary form as well as to encode binary data into Base64-encoded strings. Ruby’s Base64 module offers the encode64 method, which takes binary data as input and returns a Base64-encoded string.

The Base64 module in Ruby also provides the decode64 method, which converts a Base64-encoded string back into its original binary form. This is crucial when you receive Base64-encoded data and need to recover the original binary data.

Example of Base64 Encoding and Decoding in Ruby

Now let’s look at a very simple example of encoding and decoding.

require 'base64'
 
text = "B64Encode"
binary_data = text.encode('UTF-8').force_encoding('ASCII-8BIT')
 
# Encoding
encoded_data = Base64.encode64(binary_data)
puts "Encoded: #{encoded_data}"
 
# Decoding
decoded_data = Base64.decode64(encoded_data)
puts "Decoded: #{decoded_data}"

The script starts by requiring the base64 module, which provides methods for encoding and decoding binary data using a Base64 representation.

Next, the script defines a text variable containing the string “B64Encode”. This text is then encoded as UTF-8 and forced to be treated as binary data by calling the force_encoding method with the 'ASCII-8BIT' argument.

The script then encodes the binary data using the Base64.encode64 method, which returns a Base64 encoded string. The encoded data is printed to the console using the puts method.

Finally, the script decodes the encoded data using the Base64.decode64 method, which returns the original binary data. The decoded data is also printed to the console using the puts method.