note 66240 deleted from function.base-convert by danbrown

From: Date: Sat, 02 Jul 2011 15:40:00 +0000
Subject: note 66240 deleted from function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-181168@lists.php.net to get a copy of this message
Note Submitter: Michael Renner ---- Here is an unfucked version of the arbitrary-large-number base_convert examples below: I modified it so that it works as drop-in replacement for base_convert. Attention, no sanity checking is done for the input numbers, anything larger than 36 won't work.. function unfucked_base_convert ($numstring, $frombase, $tobase) { $chars = "0123456789abcdefghijklmnopqrstuvwxyz"; $tostring = substr($chars, 0, $tobase); $length = strlen($numstring); $result = ''; for ($i = 0; $i < $length; $i++) { $number[$i] = strpos($chars, $numstring{$i}); } do { $divide = 0; $newlen = 0; for ($i = 0; $i < $length; $i++) { $divide = $divide * $frombase + $number[$i]; if ($divide >= $tobase) { $number[$newlen++] = (int)($divide / $tobase); $divide = $divide % $tobase; } elseif ($newlen > 0) { $number[$newlen++] = 0; } } $length = $newlen; $result = $tostring{$divide} . $result; } while ($newlen != 0); return $result; }

« previous php.notes (#181168) next »