ASCII To Binary

This algorithm converts ASCII code to binary numbers.



									function ASCIIToBinary($str)
{
	$bin = "";
	$strLen = strlen($str);

	for ($i = 0; $i < $strLen; $i++)
	{
		$cBin = DecimalToBinary(ord($str[$i]));

		if (strlen($cBin) < 8)
			$cBin = str_pad($cBin, 8, "0", STR_PAD_LEFT);

		$bin .= $cBin;
	}

	return $bin;
}

function DecimalToBinary($dec)
{
	if ($dec < 1) return "0";

	$binStr = "";

	while ($dec > 0)
	{
		$binStr = substr_replace($binStr, strval($dec % 2), 0, 0);

		$dec = floor($dec / 2);
	}

	return $binStr;
}
								


Example

									$data = "Programming Algorithms";
$value = ASCIIToBinary($data);
								


Output

									01010000011100100110111101100111011100100110000101101101011011010110100101101110011001110010000001000001011011000110011101101111011100100110100101110100011010000110110101110011