Created
April 28, 2020 18:15
-
-
Save milypoint/e7f6d6f80d8992d5ad0530e38e8c5dd8 to your computer and use it in GitHub Desktop.
Compress integer to string
This file contains 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 | |
// $codeset="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
$codeset="QybVileI4aZC7PMk9v85EcA0GS63oOqfKLmpJurNHstYhgBdjTUxWD2FRw1znX"; | |
$base = strlen($codeset); | |
function intToStr($n) | |
{ | |
global $codeset, $base; | |
$converted = ""; | |
while ($n > 0) { | |
$converted = substr($codeset, ($n % $base), 1) . $converted; | |
$n = floor($n/$base); | |
} | |
return $converted; | |
} | |
function strToInt($converted) | |
{ | |
global $codeset, $base; | |
$c = 0; | |
for ($i = strlen($converted); $i; $i--) { | |
$c += strpos($codeset, substr($converted, (-1 * ( $i - strlen($converted) )),1)) | |
* pow($base,$i-1); | |
} | |
return $c; | |
} | |
echo intToStr(1337); | |
echo ' '; | |
echo strToInt(intToStr(1337)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment