Created
November 26, 2014 14:50
-
-
Save asika32764/e18f9a95bfbfecfde0b9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class BaseIntEncoder { | |
//const $codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
//readable character set excluded (0,O,1,l) | |
const codeset = "23456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; | |
static function encode($n){ | |
$base = strlen(self::codeset); | |
$converted = ''; | |
while ($n > 0) { | |
$converted = substr(self::codeset, bcmod($n,$base), 1) . $converted; | |
$n = self::bcFloor(bcdiv($n, $base)); | |
} | |
return $converted ; | |
} | |
static function decode($code){ | |
$base = strlen(self::codeset); | |
$c = '0'; | |
for ($i = strlen($code); $i; $i--) { | |
$c = bcadd($c,bcmul(strpos(self::codeset, substr($code, (-1 * ( $i - strlen($code) )),1)) | |
,bcpow($base,$i-1))); | |
} | |
return bcmul($c, 1, 0); | |
} | |
static private function bcFloor($x) | |
{ | |
return bcmul($x, '1', 0); | |
} | |
static private function bcCeil($x) | |
{ | |
$floor = bcFloor($x); | |
return bcadd($floor, ceil(bcsub($x, $floor))); | |
} | |
static private function bcRound($x) | |
{ | |
$floor = bcFloor($x); | |
return bcadd($floor, round(bcsub($x, $floor))); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
BaseIntEncoder::encode('1122344523');//result:3IcjVE | |
BaseIntEncoder::decode('3IcjVE');//result:1122344523 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment