note 95086 deleted from function.base-convert by danbrown

From: Date: Sat, 12 Dec 2009 20:12:17 +0000
Subject: note 95086 deleted from function.base-convert by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163893@lists.php.net to get a copy of this message
Note Submitter: sebastien at cslavoie dot com ---- From all your suggestions and the algorythmn on this page: http://www.cut-the-knot.org/recurrence/conversion.shtml Here is a super-fast, customizable base converter. <?php function convert_int($input, $n=62) { // n <= 62 static $arr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; if ($input < $n) return $arr[$input]; else return convert_int(floor($input/$n), $n) . $arr[$input % $n]; } function unconvert_int($input, $n=62) { // n <= 62 static $arr = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; if (strlen($input) == 0) return 0; else return unconvert_int(substr($input, 0, -1), $n) * $n + strpos($arr, substr($input, -1)); } ?> It only works with int, but you could always call it twice. Example with hexadecimal -> binary: <?php convert_int(unconvert_int("1F", 16), 2) ?> I use this snippet to compress timestamps. Like this: <?php $reference = 1230789600; // Jan 1st 2009 echo $compressed = convert_int(time() - $reference); // 21HS4 echo $timestamp = unconvert_int($c) + $reference; // 1260647688 at this moment ?>

« previous php.notes (#163893) next »