dev-resources.site
for different kinds of informations.
Base64 strings concepts in different programming language
Published at
12/23/2024
Categories
base64
programming
encoding
decoding
Author
atifwattoo
Author
10 person written this
atifwattoo
open
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.
encoding Article's
30 articles in total
Why I Built the Laravel Encoding Package I Couldn’t Find Anywhere Else
read article
From 'A' to '😊': How Programming Languages Handle Strings
read article
Base64 strings concepts in different programming language
currently reading
Secure and Scalable Encoding Made Easy with Laravel Encoder: A Complete Tutorial
read article
Encoding
read article
On Transformers and Vectors
read article
The ü/ü Conundrum
read article
Unlocking the Potential of Video Transcoding
read article
How to inverse transform both ordinal and label encoding?
read article
Introducción a Buffer en JavaScript
read article
Intl.Segmenter(): Don't use string.split() nor string.length
read article
Packing and unpacking bytes
read article
Chuw Vidf Nam sogp sogp 4.0 (Cvnss4.0) zujx goc nhinl mas hoaj
read article
The Hitchhiker's Guide to Binary-to-Text Encoding
read article
Text versus bytes
read article
Transforming Categorical Data: A Practical Guide to Handling Non-Numerical Variables for Machine Learning Algorithms.
read article
Dealing with Categorical Data: Encoding Features for ML Algorithms
read article
Application of Media Processing Technology to 4K/8K FHD Video Processing
read article
Base64's goodness
read article
How does Base64 work?
read article
Ordinal Vs One Hot Vs Label Encoding
read article
PHP: Useful Encoding and decoding Functions You Need to Know
read article
How good is my video? A (traditional) video quality metrics survey
read article
String encodings
read article
The unicode encoding system
read article
Unicode
read article
Serialization
read article
Base 64 Encoder-Decoder in Java
read article
Windows 系統上 Python çš„æ–‡å—輸出編碼
read article
UTF-8 strings in C (3/3)
read article
Featured ones: