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

将 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';
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神神的蜗牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值