<?php
// convert an input string into it's binary equivalent.
function asc2bin($inputString, $byteLength=8)
{
$binaryOutput = '';
$strSize = strlen($inputString);
for($x=0; $x<$strSize; $x++)
{
$charBin = decbin(ord($inputString{$x}));
$charBin = str_pad($charBin, $byteLength, '0', STR_PAD_LEFT);
$binaryOutput .= $charBin;
}
return $binaryOutput;
}
// convert a binary representation of a string back into it's original form.
function bin2asc($binaryInput, $byteLength=8)
{
if (strlen($binaryInput) % $byteLength)
{
return false;
}
// why run strlen() so many times in a loop? Use of constants = speed increase.
$strSize = strlen($binaryInput);
$origStr = '';
// jump between bytes.
for($x=0; $x<$strSize; $x += $byteLength)
{
// extract character's binary code
$charBinary = substr($binaryInput, $x, $byteLength);
$origStr .= chr(bindec($charBinary)); // conversion to ASCII.
}
return $origStr;
}
$inputString = "An input string, complete with punctuation!";
$binOut = asc2bin($inputString);
printf("Input String: %snBinary Version: %sn",$inputString, $binOut);
$ascOut = bin2asc($binOut);
printf("Input Binary: %snOutput ASCII: %sn",$binOut, $ascOut);
?>