note 86673 modified in function.dechex by thiago
| From: | thiago@php.net | Date: | Wed, 29 Oct 2008 11:35:52 +0000 |
| Subject: | note 86673 modified in function.dechex by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-145924@lists.php.net to get a copy of this message | ||
I was confused by dechex's size limitation. Here is my solution to the problem. It supports
much bigger values, as well as signs.
<?php
function dec_to_hex($dec)
{
$sign = ""; // suppress errors
if( $dec < 0){ $sign = "-"; $dec = abs($dec); }
$hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
11 => 'b', 12 => 'c', 13 => 'd', 14 =>
'e',
15 => 'f' );
do
{
$h = $hex[($dec%16)] . $h;
$dec /= 16;
}
while( $dec >= 1 );
return $sign . $h;
}
?>
--was--
I was confused by dechex's size limitation. Here is my solution to the problem. It supports
much bigger values, as well as signs.
function dec_to_hex($dec)
{
$sign = ""; // suppress errors
if( $dec < 0){ $sign = "-"; $dec = abs($dec); }
$hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
11 => 'b', 12 => 'c', 13 => 'd', 14 =>
'e',
15 => 'f' );
do
{
$h = $hex[($dec%16)] . $h;
$dec /= 16;
}
while( $dec >= 1 );
return $sign . $h;
}
http://php.net/manual/en/function.dechex.php