How to encode and decode base64 in Python, PHP, Java, NodeJS and C++

How to encode and decode base64 in Python, PHP, Java, NodeJS and C++
Photo by Markus Spiske / Unsplash

You can always use our free Base64 Decoder to copy and paste text between different devices

Base64 encoding is a commonly used technique for encoding binary data as ASCII text. It is often used for transmitting data over protocols that are designed to handle only ASCII characters, such as email, HTTP, or FTP. Base64 encoding works by converting every three bytes of data into a group of four ASCII characters, using a fixed set of 64 characters. Decoding reverses this process, converting the four ASCII characters back into three bytes of binary data.

In Python, PHP, Java, NodeJS, and C++, there are built-in libraries that can encode and decode Base64 data. Here are some examples:

Here is an example of how to encode and decode Base64 in Python, PHP, Java, NodeJS and C++:


import base64

# Encoding
data = b'hello world'
encoded_data = base64.b64encode(data)

# Decoding
decoded_data = base64.b64decode(encoded_data)


// Encoding
$data = 'hello world';
$encoded_data = base64_encode($data);
echo $encoded_data;

// Decoding
$decoded_data = base64_decode($encoded_data);
echo $decoded_data;


import java.util.Base64;

// Encoding
byte[] data = "hello world".getBytes("UTF-8");
String encodedData = Base64.getEncoder().encodeToString(data);

// Decoding
byte[] decodedData = Base64.getDecoder().decode(encodedData);
String decodedString = new String(decodedData, "UTF-8");


const base64 = require('base64-js');

// Encoding
const data = Buffer.from('hello world');
const encodedData = base64.fromByteArray(data);

// Decoding
const decodedData = base64.toByteArray(encodedData);


#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>

#include "base64.h"

using namespace std;

// Encoding
string data = "hello world";
string encoded_data = base64_encode(reinterpret_cast<const unsigned char*>(data.c_str()), data.length());
cout << encoded_data << endl;

// Decoding
string decoded_data = base64_decode(encoded_data);
cout << decoded_data << endl;

Visit Online Toolz, a hub containing 100+ tool for your everyday use