Base64 strings are not exclusive to Python—they are widely used across many programming languages and platforms. Base64 encoding is a universal standard defined in RFC 4648, and virtually every major programming language provides built-in or library support for working with Base64 strings.
Languages and Usage
Here’s how Base64 is supported and used in various popular languages:
1. Python
-
Module:
base64
- Use Cases: Encoding and decoding binary data, especially for APIs, file uploads, and embedding resources in HTML/JSON.
import base64
# Encode
encoded = base64.b64encode(b"Hello, Python!")
print(encoded) # Output: b'SGVsbG8sIFB5dGhvbiE='
# Decode
decoded = base64.b64decode(encoded)
print(decoded) # Output: b'Hello, Python!'
2. JavaScript
-
Methods:
btoa
(binary to ASCII),atob
(ASCII to binary) - Use Cases: Encoding data for web communication, embedding images or files in web pages.
// Encode
let data = "Hello, JavaScript!";
let encoded = btoa(data);
console.log(encoded); // Output: "SGVsbG8sIEphdmFTY3JpcHQh"
// Decode
let decoded = atob(encoded);
console.log(decoded); // Output: "Hello, JavaScript!"
3. Java
-
Library:
java.util.Base64
- Use Cases: Secure file handling, API integration, and token management.
import java.util.Base64;
public class Main {
public static void main(String[] args) {
// Encode
String original = "Hello, Java!";
String encoded = Base64.getEncoder().encodeToString(original.getBytes());
System.out.println(encoded); // Output: "SGVsbG8sIEphdmEh"
// Decode
byte[] decodedBytes = Base64.getDecoder().decode(encoded);
String decoded = new String(decodedBytes);
System.out.println(decoded); // Output: "Hello, Java!"
}
}
4. C#
-
Namespace:
System.Convert
- Use Cases: File handling, secure token exchange, and embedding data in web APIs.
using System;
class Program {
static void Main() {
// Encode
string data = "Hello, C#!";
string encoded = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(data));
Console.WriteLine(encoded); // Output: "SGVsbG8sIEMjIQ=="
// Decode
string decoded = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(encoded));
Console.WriteLine(decoded); // Output: "Hello, C#!"
}
}
5. PHP
-
Functions:
base64_encode
andbase64_decode
- Use Cases: Handling image uploads, encoding user credentials (like in Basic Authentication).
<?php
// Encode
$data = "Hello, PHP!";
$encoded = base64_encode($data);
echo $encoded; // Output: "SGVsbG8sIFBIUCE="
// Decode
$decoded = base64_decode($encoded);
echo $decoded; // Output: "Hello, PHP!"
?>
6. Ruby
-
Library:
Base64
(part of Ruby’s standard library) - Use Cases: Encoding for APIs, handling files, embedding binary data in text.
require 'base64'
# Encode
data = "Hello, Ruby!"
encoded = Base64.encode64(data)
puts encoded # Output: "SGVsbG8sIFJ1Ynkh\n"
# Decode
decoded = Base64.decode64(encoded)
puts decoded # Output: "Hello, Ruby!"
7. Go
-
Package:
encoding/base64
- Use Cases: Token management, secure file transmission, and embedding data in APIs.
package main
import (
"encoding/base64"
"fmt"
)
func main() {
// Encode
data := "Hello, Go!"
encoded := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(encoded) // Output: "SGVsbG8sIEdvIQ=="
// Decode
decoded, _ := base64.StdEncoding.DecodeString(encoded)
fmt.Println(string(decoded)) // Output: "Hello, Go!"
}
Common Applications Across Languages
-
Web APIs:
- Encoding binary files (e.g., images, audio) for transmission over REST or GraphQL APIs.
- Common in authentication tokens (e.g., JSON Web Tokens use Base64).
-
Embedded Data:
- Embedding resources in HTML (like inline images in
<img>
tags).
- Embedding resources in HTML (like inline images in
-
Email Attachments:
- Base64 is used in MIME email attachments to encode binary files.
-
File Handling:
- Converting files to Base64 strings for storage in text-based databases.
Top comments (0)