How to encode / decode Base64 in Python?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In Python, you can encode a string in Base64 using the base64
module:
- Convert the string into bytes (because Base64 works with bytes, not text).
- Use
base64.b64encode()
to encode the bytes.
- Convert the encoded bytes back into a string so it's easy to read and share.
import base64
def encode_base64(text: str) -> str:
encoded_bytes = base64.b64encode(text.encode('utf-8'))
return encoded_bytes.decode('utf-8')
# Example usage
text = "Hello, World!"
encoded_text = encode_base64(text)
print("Encoded:", encoded_text)
Decoding is the reverse process of encoding. When you receive a Base64-encoded string, you need to convert it back into its original text.
To do this in Python:
- Use
base64.b64decode()
to decode the Base64 string into bytes.
- Convert the bytes back into a regular text string using
.decode('utf-8')
.
This method allows you to safely recover the original text from a Base64-encoded message.
import base64
def decode_base64(encoded_text: str) -> str:
decoded_bytes = base64.b64decode(encoded_text)
return decoded_bytes.decode('utf-8')
# Example usage
encoded_text = "SGVsbG8sIFdvcmxkIQ=="
decoded_text = decode_base64(encoded_text)
print("Decoded:", decoded_text)
How to encode / decode Base64 in Java?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
Base64 encoding in Java is done using the Base64
class from java.util
. The process is simple:
- Convert the string into bytes (because Base64 works with bytes).
- Use
Base64.getEncoder().encodeToString()
to encode the bytes into a Base64 string.
- The output is a string that contains only letters, numbers, and a few special characters (
+
, /
, and =
for padding).
import java.util.Base64;
public class Base64Encoder {
public static void main(String[] args) {
String text = "Hello, World!";
String encodedText = Base64.getEncoder().encodeToString(text.getBytes());
System.out.println("Encoded: " + encodedText);
}
}
Decoding is the opposite of encoding. You take a Base64 string and convert it back to its original text.
- Use
Base64.getDecoder().decode()
to convert the Base64 string back into bytes.
- Convert the bytes into a normal string using
new String()
.
import java.util.Base64;
public class Base64Decoder {
public static void main(String[] args) {
String encodedText = "SGVsbG8sIFdvcmxkIQ==";
String decodedText = new String(Base64.getDecoder().decode(encodedText));
System.out.println("Decoded: " + decodedText);
}
}
How to encode / decode Base64 in C#?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In C#, you can use the Convert.ToBase64String()
method to encode a string into Base64.
- Convert the string into bytes using
Encoding.UTF8.GetBytes()
.
- Use
Convert.ToBase64String()
to encode the byte array int
using System;
using System.Text;
class Base64Encoder
{
static void Main()
{
string text = "Hello, World!";
string encodedText = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
Console.WriteLine("Encoded: " + encodedText);
}
}
Decoding follows the reverse process:
- Use
Convert.FromBase64String()
to convert the Base64 string back into a byte array.
- Convert the byte array into a readable string using
Encoding.UTF8.GetString()
.
using System;
using System.Text;
class Base64Decoder
{
static void Main()
{
string encodedText = "SGVsbG8sIFdvcmxkIQ==";
string decodedText = Encoding.UTF8.GetString(Convert.FromBase64String(encodedText));
Console.WriteLine("Decoded: " + decodedText);
}
}
How to encode / decode Base64 in NodeJS?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In Node.js, the Buffer
class is used for working with binary data. Base64 encoding is super easy:
- Convert the string into a
Buffer
.
- Use
.toString('base64')
to encode it into Base64.
const text = "Hello, World!";
const encodedText = Buffer.from(text, 'utf-8').toString('base64');
console.log("Encoded:", encodedText);
To decode Base64 in Node.js:
- Create a
Buffer
from the Base64 string using 'base64'
as the encoding.
- Use
.toString('utf-8')
to turn the buffer back into readable text.
const encodedText = "SGVsbG8sIFdvcmxkIQ==";
const decodedText = Buffer.from(encodedText, 'base64').toString('utf-8');
console.log("Decoded:", decodedText);
How to encode / decode Base64 in Golang?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In Go, the encoding/base64
package handles Base64 encoding.
- Convert the string to bytes (Go strings are already easy to convert with
[]byte
).
- Use
base64.StdEncoding.EncodeToString()
to encode the bytes.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
text := "Hello, World!"
encodedText := base64.StdEncoding.EncodeToString([]byte(text))
fmt.Println("Encoded:", encodedText)
}
To decode Base64:
- Use
base64.StdEncoding.DecodeString()
to turn the Base64 string back into bytes.
- Convert the bytes to a regular string.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
encodedText := "SGVsbG8sIFdvcmxkIQ=="
decodedBytes, err := base64.StdEncoding.DecodeString(encodedText)
if err != nil {
fmt.Println("Error decoding:", err)
return
}
decodedText := string(decodedBytes)
fmt.Println("Decoded:", decodedText)
}
How to encode / decode Base64 in PHP?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
PHP makes Base64 encoding super easy using the built-in base64_encode()
function.
- Just pass your string to
base64_encode()
.
- It returns a Base64-encoded version of that string.
<?php
$text = "Hello, World!";
$encodedText = base64_encode($text);
echo "Encoded: " . $encodedText;
?>
To decode a Base64 string, you use base64_decode()
.
- Pass the encoded string to
base64_decode()
.
- It returns the original text.
<?php
$encodedText = "SGVsbG8sIFdvcmxkIQ==";
$decodedText = base64_decode($encodedText);
echo "Decoded: " . $decodedText;
?>
How to encode / decode Base64 in Ruby?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
Ruby has built-in support for Base64 via the base64
module in the standard library.
- First, require the
base64
module.
- Use
Base64.strict_encode64()
to encode a string into Base64 encoded string.
require 'base64'
text = "Hello, World!"
encoded_text = Base64.strict_encode64(text)
puts "Encoded: #{encoded_text}"
To decode a Base64 string:
- Use
Base64.decode64()
to convert the encoded string back to its original form.
require 'base64'
encoded_text = "SGVsbG8sIFdvcmxkIQ=="
decoded_text = Base64.decode64(encoded_text)
puts "Decoded: #{decoded_text}"
How to encode / decode Base64 in Swift?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In Swift, Base64 encoding is built into the Data
type.
- Convert your
String
into Data
using UTF-8 encoding.
- Call
.base64EncodedString()
on the Data
to get the Base64 string.
import Foundation
let text = "Hello, World!"
if let data = text.data(using: .utf8) {
let encodedText = data.base64EncodedString()
print("Encoded: \(encodedText)")
}
To decode a Base64 string:
- Create
Data
from the Base64 string using Data(base64Encoded:)
.
- Convert the
Data
back into a String
.
import Foundation
let encodedText = "SGVsbG8sIFdvcmxkIQ=="
if let data = Data(base64Encoded: encodedText),
let decodedText = String(data: data, encoding: .utf8) {
print("Decoded: \(decodedText)")
}
How to encode / decode Base64 in Kotlin?
Base64 encoding is a way to convert text (or any binary data) into a format that only contains letters, numbers, and a few special characters (+
, /
, and =
for padding). This is useful when you need to store or transfer data safely over systems that might not support special characters.
In Kotlin, you can use Base64
from Java's standard library (java.util.Base64
).
- Convert the string to a
ByteArray
using UTF-8.
- Use
Base64.getEncoder().encodeToString()
to encode it.
import java.util.Base64
fun main() {
val text = "Hello, World!"
val encodedText = Base64.getEncoder().encodeToString(text.toByteArray(Charsets.UTF_8))
println("Encoded: $encodedText")
}
To decode a Base64 string:
- Use
Base64.getDecoder().decode()
to turn the string back into a byte array.
- Convert the byte array back into a regular string.
import java.util.Base64
fun main() {
val encodedText = "SGVsbG8sIFdvcmxkIQ=="
val decodedBytes = Base64.getDecoder().decode(encodedText)
val decodedText = String(decodedBytes, Charsets.UTF_8)
println("Decoded: $decodedText")
}