/** * Normalize a number and convert to binary. * * @param $d number * A bipolar number. * @return A binary number stored as a double */ function ToNormalizedBinary($d) { return normalizeBinary(toBinary($d)); }
<?php function toBinary($num, $binary) { $length = strlen($binary); while ($num > $length - 1) { $out = $binary[fmod($num, $length)] . $out; $num = floor($num / $length); } return $binary[$num] . $out; } function toBase($str, $binary) { $size = strlen($str) - 1; $length = strlen($binary); $str = str_split($str); $out = strpos($binary, array_pop($str)); foreach ($str as $i => $char) { $out += strpos($binary, $char) * pow($length, $size - $i); } return $out; } $binary = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef'; echo toBinary(4294967295.0, $binary), ' = ', toBase('WaB6U3', $binary);