note 52123 deleted from function.dechex by googleguy
| From: | googleguy@php.net | Date: | Tue, 22 Oct 2013 10:18:25 +0000 |
| Subject: | note 52123 deleted from function.dechex by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-196996@lists.php.net to get a copy of this message | ||
Note Submitter: matt dot smith at email dot ky
----
It took me hours, but I've finally figured it all out...
--
To figure up how many lines it'll take to get from,
0000 to FFFF, use this formula:
((hex_length / 2) * 255) + 1
For 0000 to FFFF, this would be: ((4 / 2) * 255) + 1 = 511
For 0000000000 to FFFFFFFFFF, this would be: ((10 / 2) * 255) + 1 = 1276
--
To figure up how many possibilities a certain hex length will give (such as that of an MD5 hash, for
example), use this formula:
256^(hex_length / 2)
Like so:
hex = 0123ABCDEF
hex_length = 10
possibilities = 256^(10 / 2)
possibilities = 256^5
possibilities = 1,099,511,627,776
--
And I don't know how useful this next part is, but it's quite nifty. Just set $length to
be your hex_length and it'll display all "incrementally-possible" hexadecimals, along
with line numbers:
<?php
$length = 10; // Change this to the total length of your hexadecimal
function array_check($array1, $array2)
{
$count1 = count($array1);
$count2 = count($array2);
if ($count1 != $count2)
return FALSE;
$match = 0;
for ($i = 0; $i < $count1; $i++)
if ($array1[$i] == $array2[$i])
$match++;
if ($match != $count1)
return FALSE;
return TRUE;
}
function zeropad($num)
{
return (strlen($num) == 1) ? '0'.$num : $num;
}
function zeropad_lineno($num, $length)
{
while (strlen($num) < $length)
$num = '0'.$num;
return $num;
}
$hexadecimals = $length / 2;
$possibilities = /*number_format(*/ pow(256, $hexadecimals) /*)*/;
echo "A hexadecimal of this length ({$length}) has {$possibilities}
possibilities.<br>\n";
echo "<br>\n";
//init
for ($i = 0; $i < $hexadecimals; $i++)
{
$x[] = '00';
$xint[] = 0;
$end[] = 'ff';
}
$line = 0;
while ( !(array_check($x, $end)) ) // while we're not currently at the end (FF..)
{
$hexstr = '';
for ($i = 0; $i < $hexadecimals; $i++)
{
$x[$i] = dechex($xint[$i]);
$hexstr .= zeropad(dechex($xint[$i]));
}
$line++;
$linestr = zeropad_lineno($line, 8);
echo "<code>{$linestr}: {$hexstr}</code><br>\n";
// hex increment
for ($i = ($hexadecimals - 1); $i >= 0; $i--)
{
// increment bit if possible
if ($xint[$i] != 255)
{
// increment bit and then break out of the for loop
$xint[$i]++;
break;
}
}
}
?>