Base64 strings concepts in different programming language
December 23, 2024

Base64 strings concepts in different programming language

Base64 strings are not unique to Python; they are widely used in many programming languages ​​and platforms. Base64 encoding is a universal standard defined in RFC 4648almost all major programming languages ​​provide built-in or library support for processing Base64 strings.


Language and usage

The following is the support and use of Base64 in various popular languages:



1. Python

  • module: base64
  • Use cases: Encoding and decoding binary data, specifically 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 full screen mode

Exit full screen mode



2. JavaScript

  • method: btoa (binary to ASCII), atob (ASCII to binary)
  • Use cases: Encode network communication data and embed 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 full screen mode

Exit full screen 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 full screen mode

Exit full screen mode



4. C#

  • namespace: System.Convert
  • Use cases: File handling, security 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 full screen mode

Exit full screen mode



5. PHP

  • Function: base64_encode and base64_decode
  • Use cases: Handles image uploads and encodes user credentials (such as basic authentication).

// Encode
$data = "Hello, PHP!";
$encoded = base64_encode($data);
echo $encoded; // Output: "SGVsbG8sIFBIUCE="

// Decode
$decoded = base64_decode($encoded);
echo $decoded; // Output: "Hello, PHP!"
?>
Enter full screen mode

Exit full screen mode



6. ruby

  • library: Base64 (Part of the Ruby standard library)
  • Use cases: API coding, processing 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 full screen mode

Exit full screen mode



7. go

  • pack: encoding/base64
  • Use cases: Token management, secure file transfer, 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 full screen mode

Exit full screen mode



Common apps across languages

  1. Network API:

    • Encode binaries (e.g. images, audio) for transmission via REST or GraphQL APIs.
    • Commonly used in authentication tokens (for example, JSON web tokens use Base64).
  2. Embed data:

    • Embed resources in HTML (such as inline images) Label).
  3. Email attachment:

    • Base64 is used to encode binary files in MIME email attachments.
  4. File processing:

    • Convert files to Base64 strings for storage in text-based databases.

2024-12-23 15:04:10

Leave a Reply

Your email address will not be published. Required fields are marked *