您的位置:首页 > 其它

将int,bigint整型数值可逆转换字符串

2016-04-08 17:06 399 查看
将 Int 和 BigInt 类型数值转换为字符串的可逆方法,可用于缩短网址或记录的ID转换等。

如: 9223372036854775807 => aZl8N0y58M7

class Convert
{
/**
* 默认密钥字符串
* @var string
*/
const KEY = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

/**
* 将 Int 类型十进制数值转换为指定进制编码
* @param int|string $num 取值范围 0 ~ 2147483647 之间
* @return string
*/
public static function encodeInt($num) {
$str = '';
if ($num <= 0)
$str = substr(self::KEY, 0, 1);
while ($num > 0) {
$val = intval($num / 62);
$mod = $num % 62;
$str = substr(self::KEY, $mod, 1) . $str;
$num = $val;
}
return $str;
}

/**
* 将编码字符串转换为 Int 类整型数值
* @param string $code
* @return int
*/
public static function decodeInt($code){
$result = null;
$len = strlen($code);
for ($i = 1; $i <= $len; $i++) {
$char = substr($code, $i - 1, 1);
$result += intval(strpos(self::KEY, $char)) * pow(62, $len - $i);
}
return $result;
}

/**
* 支持15位长度的整型,超过则精度大幅降低
* @param int $num
* @return string
*/
public static function encodeInt2($num) {
$out = '';
for ($t = floor(log10($num)/log10(62)); $t >= 0; $t--) {
$a = floor($num/bcpow(62, $t));
$out = $out . substr(self::KEY, $a, 1);
$num = $num - $a * pow(62, $t);
}
return $out;
}

/**
* 支持最大15位整型字符串的解码
* @param string $num
* @return string
*/
public static function decodeInt2($num) {
$out = 0;
$len = strlen($num) - 1;
for ($t = 0; $t <= $len; $t++) {
$out = $out + strpos(self::KEY, substr( $num, $t, 1 )) * pow(62, $len - $t);
}
return $out;
}

/**
* 将 BigInt 类型的数值转换为指定进制值
* @param int|string $num
* @return string
*/
public static function encodeBigInt($num) {
bcscale(0);
$str = '';
if ($num <= 0)
$str = substr(self::KEY, 0, 1);
while ($num > 0) {
$div = bcdiv($num, 62);
$mod = bcmod($num, 62);
$str = substr(self::KEY, $mod, 1) . $str;
$num = $div;
}
return $str;
}

/**
* 将编码字符串转换为 BigInt 类整型数值
* @param string $code
* @return string
*/
public static function decodeBigInt($code) {
bcscale(0);
$result = '';
$len = strlen($code);
for ($i = 1; $i <= $len; $i++) {
$char = substr($code, $i - 1, 1);
$result = bcadd(bcmul(strpos(self::KEY, $char), bcpow(62, $len - $i)), $result);
}
return $result;
}
}


测试方法:



结果:



echo 'Begin ~~<br><hr><br>';
$begin = microtime(true);
$bm = memory_get_usage();

$j = 0;
$cv = new Convert();
//for ($i = 0; $i < 10000; $i++) {
$raw = '9223372036854775807';//rand(200000, 214748) . rand(1000, 3648);
$encode = $cv->encodeBigInt($raw);
$decode = $cv->decodeBigInt($encode);
//    $encode = $is->encodeInt($raw);
//    $decode = number_format($is->decodeInt($encode), 0, '', '');
if ($raw != $decode) {
$j++;
//        echo '<script>alert("not same");</script>';
}
//}
echo '原文: '. $raw .' 长度: '. strlen($raw) .'<br>';
echo '编码: '. $encode .' 长度: '. strlen($encode) .'<br>';
echo '解码: '. $decode .'<br>';
echo '不一致:'. $j;
echo '<br>';

$em = memory_get_usage();

echo 'End !<br><hr><br>Use Time :';
echo microtime(true) - $begin;
echo '<br><hr><br>Use Mem:';
echo ceil(($em - $bm)/1024/1024) .'MB';
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: