Base32 encode and decode function in PHP
In PHP, you can use the base32_encode
and base32_decode
functions to encode and decode data in Base32 format. If you’re using PHP 5.6 or later, these functions are available by default.
The base32_encode
function starts by converting each character in the input string to its ASCII value and then converts that ASCII value to an 8-bit binary representation. This results in a long binary string.
The base32_decode
function starts by mapping each Base32 character in the input string to its 5-bit binary representation. This is done by finding the index of the character in the predefined set of Base32 characters.
Here’s an example:
Base32 Encode in PHP:
1 2 3 4 |
$data = "Hello, World!"; $encodedData = base32_encode($data); echo "Encoded: $encodedData"; |
Base32 Decode in PHP:
1 2 3 4 |
$encodedData = "JBSWY3DPEBLW64TMMQ======"; $decodedData = base32_decode($encodedData); echo "Decoded: $decodedData"; |