note 105213 added to function.dechex
| From: | antonio dot daeliminare dot bonifati at gmail dot com | Date: | Wed, 03 Aug 2011 09:00:26 +0000 |
| Subject: | note 105213 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182131@lists.php.net to get a copy of this message | ||
jbleau algorithm is right, but it still uses 32 bit integers so that overflow will happen just like
in dechex. All operations must be done using BCMath functions. Here is a correct version:
<?php
# Replacement for standard PHP function dechex. It supports much bigger values.
function dec2hex($dec)
{
$h = 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'
);
$hex = '';
do
{
# We could also use dechex here instead of the $h array,
# but then we would need strtoupper as well.
$hex = $h[bcmod($dec, 16)] . $hex;
$dec = bcdiv($dec, 16, 0);
}
while (bccomp($dec, 1) != -1); # That is $dec >= 1
return $hex;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 92.27.17.145
----
Manual Page -- http://www.php.net/manual/en/function.dechex.php
Edit -- https://master.php.net/note/edit/105213
Del: integrated -- https://master.php.net/note/delete/105213/integrated
Del: useless -- https://master.php.net/note/delete/105213/useless
Del: bad code -- https://master.php.net/note/delete/105213/bad+code
Del: spam -- https://master.php.net/note/delete/105213/spam
Del: non-english -- https://master.php.net/note/delete/105213/non-english
Del: in docs -- https://master.php.net/note/delete/105213/in+docs
Del: other reasons-- https://master.php.net/note/delete/105213
Reject -- https://master.php.net/note/reject/105213
Search -- https://master.php.net/manage/user-notes.php