Logo

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 concepts in different programming language

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!'
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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!"
    }
}
Enter fullscreen mode Exit fullscreen mode

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#!"
    }
}
Enter fullscreen mode Exit fullscreen mode

5. PHP

  • Functions: base64_encode and base64_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!"
?>
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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!"
}
Enter fullscreen mode Exit fullscreen mode

Common Applications Across Languages

  1. 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).
  2. Embedded Data:
    • Embedding resources in HTML (like inline images in <img> tags).
  3. Email Attachments:
    • Base64 is used in MIME email attachments to encode binary files.
  4. File Handling:
    • Converting files to Base64 strings for storage in text-based databases.
encoding Article's
30 articles in total
Favicon
Why I Built the Laravel Encoding Package I Couldn’t Find Anywhere Else
Favicon
From 'A' to '😊': How Programming Languages Handle Strings
Favicon
Base64 strings concepts in different programming language
Favicon
Secure and Scalable Encoding Made Easy with Laravel Encoder: A Complete Tutorial
Favicon
Encoding
Favicon
On Transformers and Vectors
Favicon
The ü/ü Conundrum
Favicon
Unlocking the Potential of Video Transcoding
Favicon
How to inverse transform both ordinal and label encoding?
Favicon
Introducción a Buffer en JavaScript
Favicon
Intl.Segmenter(): Don't use string.split() nor string.length
Favicon
Packing and unpacking bytes
Favicon
Chuw Vidf Nam sogp sogp 4.0 (Cvnss4.0) zujx goc nhinl mas hoaj
Favicon
The Hitchhiker's Guide to Binary-to-Text Encoding
Favicon
Text versus bytes
Favicon
Transforming Categorical Data: A Practical Guide to Handling Non-Numerical Variables for Machine Learning Algorithms.
Favicon
Dealing with Categorical Data: Encoding Features for ML Algorithms
Favicon
Application of Media Processing Technology to 4K/8K FHD Video Processing
Favicon
Base64's goodness
Favicon
How does Base64 work?
Favicon
Ordinal Vs One Hot Vs Label Encoding
Favicon
PHP: Useful Encoding and decoding Functions You Need to Know
Favicon
How good is my video? A (traditional) video quality metrics survey
Favicon
String encodings
Favicon
The unicode encoding system
Favicon
Unicode
Favicon
Serialization
Favicon
Base 64 Encoder-Decoder in Java
Favicon
Windows 系統上 Python 的文字輸出編碼
Favicon
UTF-8 strings in C (3/3)

Featured ones: