XhCode Online Converter Tools

Base64 Encode / Decode



Base64 Encode / Decode

Base64 encoding is a way of encoding binary data into an ASCII string format. It is commonly used for transmitting binary data over media that are designed to deal with text (like email or URLs). It takes the original data (binary or text) and converts it into a readable string of characters, which can be safely used in text-based formats.

Base64 decoding is the reverse process of converting the encoded string back into its original binary format.

How Base64 Encoding and Decoding Work
Base64 Encoding:
Base64 takes every 3 bytes (24 bits) of data and divides it into 4 groups of 6 bits.
Each group of 6 bits is then mapped to one of 64 different ASCII characters (hence the name Base64).
Base64 Decoding:
The encoded string is taken, and each of the 6-bit groups is mapped back to its original byte.
The bytes are then reconstructed into the original binary or text data.
Example:
Base64 Encoding Example:
Let's say we want to encode the text Hello World.

Original text: Hello World
Convert it into ASCII values:
rust

H -> 72
e -> 101
l -> 108
l -> 108
o -> 111
(space) -> 32
W -> 87
o -> 111
r -> 114
l -> 108
d -> 100
Convert these ASCII values to binary:
rust

72 -> 01001000
101 -> 01100101
108 -> 01101100
108 -> 01101100
111 -> 01101111
(space) -> 00100000
87 -> 01010111
111 -> 01101111
114 -> 01110010
108 -> 01101100
100 -> 01100100
Group the binary into 6-bit chunks and map to the Base64 alphabet.
The encoded string would be:
makefile

SGVsbG8gV29ybGQ=
Base64 Decoding Example:
To decode the Base64 string SGVsbG8gV29ybGQ=, you would:

Convert the Base64 string back to binary.
Group the 6-bit chunks and convert them to their original 8-bit byte values.
Reconstruct the original text Hello World.
Base64 Encode / Decode in Code
You can use various programming languages to encode or decode Base64. Here are examples in popular languages.

1. JavaScript:
Encoding:
javascript

let originalString = "Hello World";
let encodedString = btoa(originalString);
console.log(encodedString); // Outputs: SGVsbG8gV29ybGQ=
Decoding:
javascript

let encodedString = "SGVsbG8gV29ybGQ=";
let decodedString = atob(encodedString);
console.log(decodedString); // Outputs: Hello World
2. Python:
Encoding:
python

import base64
original_string = "Hello World"
encoded_string = base64.b64encode(original_string.encode())
print(encoded_string.decode()) # Outputs: SGVsbG8gV29ybGQ=
Decoding:
python

import base64
encoded_string = "SGVsbG8gV29ybGQ="
decoded_string = base64.b64decode(encoded_string).decode()
print(decoded_string) # Outputs: Hello World
3. Online Tools:
If you don't want to write code, there are many online tools available to easily encode and decode Base64:

Base64 Encode/Decode Tool
Base64 Guru
You can paste your string and click on the encode or decode buttons to convert it.

Summary of Base64 Encoding & Decoding
Base64 Encoding: Converts binary data into a text-based string using ASCII characters.
Base64 Decoding: Converts the Base64 encoded string back into its original binary format.
Use Cases: Common in email encoding, data embedding (e.g., images in HTML), URL encoding, etc.