The function 'base_convert' looks like it would be perfect for converting a hexadecimal number to a binary number. That code should look like :
<?php
// Author: holdoffhunger@gmail.com
$hexadecimal_number = "1234567890aBcDeF";
$binary_number = base_convert($hexadecimal_number, 16, 2);
print($binary_number);
?>
However, if the hexadecimal number is too large (more than ~40 characters), the result is always a string of zeros as the binary result. So, I wrote some simple code to specifically handle base conversion with Hexadecimal-to-Binary in PHP :
<?php
// Author: holdoffhunger@gmail.com
// Example Hexadecimal
// ---------------------------------------------
$hexadecimal_number = "1234567890aBcDeF";
// Format Hexadecimal
// ---------------------------------------------
$hexadecimal_to_convert_to_binary_testable = strtolower($hexadecimal_number);
// Get String Length of Hexadecimal
// ---------------------------------------------
$length_of_text_to_convert_to_binary = strlen($hexadecimal_number);
// Initialize Result Variable
// ---------------------------------------------
$results_of_hexadecimal_to_binary_conversion = "";
// Parse Hexadecimal String
// ---------------------------------------------
for($i = 0; $i < $length_of_text_to_convert_to_binary; $i++)
{
$current_char_of_hexadecimal_for_conversion = $hexadecimal_to_convert_to_binary_testable[$i];
switch($current_char_of_hexadecimal_for_conversion)
{
case '0':
$results_of_hexadecimal_to_binary_conversion .= "0000";
break;
case '1':
$results_of_hexadecimal_to_binary_conversion .= "0001";
break;
case '2':
$results_of_hexadecimal_to_binary_conversion .= "0010";
break;
case '3':
$results_of_hexadecimal_to_binary_conversion .= "0011";
break;
case '4':
$results_of_hexadecimal_to_binary_conversion .= "0100";
break;
case '5':
$results_of_hexadecimal_to_binary_conversion .= "0101";
break;
case '6':
$results_of_hexadecimal_to_binary_conversion .= "0110";
break;
case '7':
$results_of_hexadecimal_to_binary_conversion .= "0111";
break;
case '8':
$results_of_hexadecimal_to_binary_conversion .= "1000";
break;
case '9':
$results_of_hexadecimal_to_binary_conversion .= "1001";
break;
case 'a':
$results_of_hexadecimal_to_binary_conversion .= "1010";
break;
case 'b':
$results_of_hexadecimal_to_binary_conversion .= "1011";
break;
case 'c':
$results_of_hexadecimal_to_binary_conversion .= "1100";
break;
case 'd':
$results_of_hexadecimal_to_binary_conversion .= "1101";
break;
case 'e':
$results_of_hexadecimal_to_binary_conversion .= "1110";
break;
case 'f':
$results_of_hexadecimal_to_binary_conversion .= "1111";
break;
}
}
// Print Results
// ---------------------------------------------
print($results_of_hexadecimal_to_binary_conversion);
// Output: (linebreak doesn't appear in results)
// 00010010001101000101011001111000
// 10010000101010111100110111101111
?>
Official Function Page: http://www.php.net/manual/en/function.base-convert.php
// Note: All code appearing on the PHP Revolution blog by the blog owner is released under the Hacktivismo Enhanced-Source Software License Agreement (HESSLA), unless otherwise noted. http://www.hacktivismo.com/about/hessla.php
No comments:
Post a Comment