note 105213 deleted from function.dechex by danbrown

From: Date: Wed, 03 Aug 2011 13:21:22 +0000
Subject: note 105213 deleted from function.dechex by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-182169@lists.php.net to get a copy of this message
Note Submitter: antonio dot daeliminare dot bonifati at gmail dot com ---- 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; } ?>

« previous php.notes (#182169) next »