How to encode and decode base64 in Python, PHP, Java, NodeJS and C++
You can always use our free Base64 Decoder to copy and paste text between different devices
https://www.online-toolz.com/tools/online-clipboard-copy-paste-text-links.php
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++:
Python
import base64
# Encoding
data = b'hello world'
encoded_data = base64.b64encode(data)
print(encoded_data)
# Decoding
decoded_data = base64.b64decode(encoded_data)
print(decoded_data)
PHP
// Encoding
$data = 'hello world';
$encoded_data = base64_encode($data);
echo $encoded_data;
// Decoding
$decoded_data = base64_decode($encoded_data);
echo $decoded_data;
Java
import java.util.Base64;
// Encoding
byte[] data = "hello world".getBytes("UTF-8");
String encodedData = Base64.getEncoder().encodeToString(data);
System.out.println(encodedData);
// Decoding
byte[] decodedData = Base64.getDecoder().decode(encodedData);
String decodedString = new String(decodedData, "UTF-8");
System.out.println(decodedString);
NodeJS
const base64 = require('base64-js');
// Encoding
const data = Buffer.from('hello world');
const encodedData = base64.fromByteArray(data);
console.log(encodedData);
// Decoding
const decodedData = base64.toByteArray(encodedData);
console.log(decodedData.toString());
C++
#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;