将int,bigint整型数值可逆转换字符串
来源:互联网 发布:淘宝如何申请直播间 编辑:程序博客网 时间:2024/05/21 07:17
将 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 0
- 将int,bigint整型数值可逆转换字符串
- 将char字符转换成int整型,字符串与整型之间的相互转换
- 字符串转换成整型int
- c++ 将16进制字符串转换成为int数值
- c++ 将16进制字符串转换成为int数值
- 000034:将所有整型数值全部转换成int型输出
- mysql整型数值范围区分INTEGER, INT, SMALLINT, TINYINT, MEDIUMINT, BIGINT,tinyint(1) tinyint(3)
- 十六进制字符串转换成整型数值
- 将字符串转换为数值
- 将一个整型转换为整型数组例如Int num = 5546798;转为int [] {5,5,4,6,7,9,8}(不能转换为字符串)
- 数值型字符串转换成int类型
- 将int类型大于255的数值转换为十六进制字符串
- 最近听到一个面试题,将数值型字符串转换成int类型
- strtol() -- 将字符串转换成长整型数
- atoi() -- 将字符串转换成整型数
- 将整型数转换成字符串
- javascript 将字符串类型转换为整型
- 将字符串转换为整型的函数
- 廖雪峰的Git教程
- 【文本处理】Add_SpaceTab_eveLine
- z-index不起作用原因
- Android获取本地图片、视频缩略图
- 从重载与重写来看OOP的三大特性:继承、多态、封装。
- 将int,bigint整型数值可逆转换字符串
- 底部滑动菜单实现
- Unity读取本地图片资源
- 【文本处理】Dele_SpaceTab_eveline
- oracle函数listagg的使用说明
- java condition使用及分析
- Linux内核编译的一个错误解决 ump/common/ump_kernel_common.o
- 连续特征进行离散化的方法介绍与应用例子
- 三.linux学习笔记-文件搜索命令