note 109155 added to function.base-convert
| From: | holdoffhunger at gmail dot com | Date: | Sun, 24 Jun 2012 12:29:39 +0000 |
| Subject: | note 109155 added to function.base-convert | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188774@lists.php.net to get a copy of this message | ||
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
?>
----
Server IP: 69.147.83.197
Probable Submitter: 68.9.137.178
----
Manual Page -- http://www.php.net/manual/en/function.base-convert.php
Edit -- https://master.php.net/note/edit/109155
Del: integrated -- https://master.php.net/note/delete/109155/integrated
Del: useless -- https://master.php.net/note/delete/109155/useless
Del: bad code -- https://master.php.net/note/delete/109155/bad+code
Del: spam -- https://master.php.net/note/delete/109155/spam
Del: non-english -- https://master.php.net/note/delete/109155/non-english
Del: in docs -- https://master.php.net/note/delete/109155/in+docs
Del: other reasons-- https://master.php.net/note/delete/109155
Reject -- https://master.php.net/note/reject/109155
Search -- https://master.php.net/manage/user-notes.php