Data Encodings - Herong's Tutorial Examples - Version 5.13, by Dr. Herong Yang
Implementation of Base64 in PHP - Test
This section provides a test program for the default PHP implementation of the Base64 encoding algorithm - base64_encode() and base64_decode() functions.
The Base64 encoding algorithm bas been implemented in PHP language as 2 built-in functions since PHP 4:
To test these built-in functions, I wrote this simple testing program:
<?php # PHP_Base64_Test.php # Copyright (c) 2007 by Dr. Herong Yang, http://www.herongyang.com/ # echo "\nTest 1:\n"; test("A", "QQ=="); echo "\nTest 2:\n"; test("AB", "QUI="); echo "\nTest 3:\n"; test("ABC", "QUJD"); function test($theInput, $theExpected) { $theEncoded = base64_encode($theInput); $theDecoded = base64_decode($theEncoded); echo " Input : $theInput\n"; echo " Encoded : $theEncoded\n"; echo " Expected: $theExpected\n"; echo " Decoded : $theDecoded\n"; } ?>
Here is the test result:
C:\herong\base64>php PHP_Base64_Test.php Test 1: Input : A Encoded : QQ== Expected: QQ== Decoded : A Test 2: Input : AB Encoded : QUI= Expected: QUI= Decoded : AB Test 3: Input : ABC Encoded : QUJD Expected: QUJD Decoded : ABC
The result matches my expectation perfectly.
Table of Contents
W3C Implementation of Base64 in Java
Sun Implementation of Base64 in Java
Sun Implementation of Base64 in Java - Test
Goetz' Implementation of Base64 in JavaScript
Goetz' Implementation of Base64 in JavaScript - Test
►Implementation of Base64 in PHP - Test