DEV Community

Muhammad Atif Iqbal
Muhammad Atif Iqbal

Posted on

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.

Top comments (0)