note 21086 added to function.dechex
| From: | joost at bingopaleis dot com | Date: | Sun, 28 Apr 2002 13:21:06 +0000 |
| Subject: | note 21086 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-29944@lists.php.net to get a copy of this message | ||
Here are two functions that will convert large dec numbers to hex and vice versa. And I really mean
LARGE, much larger than any function posted earlier.
<pre>
// Input: A decimal number as a String.
// Output: The equivalent hexadecimal number as a String.
function dec2hex($number)
{
$hexvalues =
array('0','1','2','3','4','5','6','7',
'8','9','A','B','C','D','E','F');
$hexval = '';
while($number != '0')
{
$hexval = $hexvalues[bcmod($number,'16')].$hexval;
$number = bcdiv($number,'16',0);
}
return $hexval;
}
// Input: A hexadecimal number as a String.
// Output: The equivalent decimal number as a String.
function hex2dec($number)
{
$decvalues = array('0' => '0', '1' => '1',
'2' => '2',
'3' => '3', '4' => '4', '5' =>
'5',
'6' => '6', '7' => '7', '8' =>
'8',
'9' => '9', 'A' => '10', 'B' =>
'11',
'C' => '12', 'D' => '13', 'E' =>
'14',
'F' => '15');
$decval = '0';
$number = strrev($number);
for($i = 0; $i < strlen($number); $i++)
{
$decval = bcadd(bcmul(bcpow('16',$i,0),$decvalues[$number{$i}]), $decval);
}
return $decval;
}
</pre>
--
http://www.php.net/manual/en/function.dechex.php
http://master.php.net/manage/user-notes.php?action=edit+21086
http://master.php.net/manage/user-notes.php?action=delete+21086
http://master.php.net/manage/user-notes.php?action=reject+21086