将int,bigint整型数值可逆转换字符串
2016-04-08 17:06
399 查看
将 Int 和 BigInt 类型数值转换为字符串的可逆方法,可用于缩短网址或记录的ID转换等。
如: 9223372036854775807 => aZl8N0y58M7
测试方法:
结果:
如: 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';
相关文章推荐
- 从重载与重写来看OOP的三大特性:继承、多态、封装。
- 优化SQL查询:如何写出高性能SQL语句
- 数据挖掘经典算法概述以及详解链接
- 【文本处理】Add_SpaceTab_eveLine
- Android读写文件权限
- 正则表达式
- 底部滑动菜单实现
- Android获取本地图片、视频缩略图
- svn安装和配置
- 廖雪峰的Git教程
- HOSVD(高阶奇异值分解)/TD分解
- LANMP配置
- 浅谈序列化
- input-group+navs
- jquery背景backgroundPosition插件
- dubbo SimpleMonitor写入日志细节
- Ubuntu中apt-get报错
- 通俗理解LDA主题模型
- rocketMQ主从问题
- Chart.js绘图,数据图