设为首页收藏本站淘宝杂货铺

从F到0 - From F to 0

 找回密码
 注册已关闭
搜索
查看: 2556|回复: 1
收起左侧

[PHP/ASP/JSP] PHP实现2~256进制任意转换(含2~62进制数字、大小写字母/Base2~62编解码程序算法全套)

[复制链接]
发表于 2019-3-19 19:13:00 | 显示全部楼层 |阅读模式


  1. <?php
  2. /*
  3. 2~256进制bin数据转换
  4. 如果需要对数字、大小写字母进行进制转换,则需要先对字符串进行以下编码:
  5. 0~9转换成chr(0)~chr(9),
  6. A~Z转换成chr(10)~chr(35)
  7. a~z转换成chr(36)~chr(61)
  8. 转换后解码即可,否则将无法正常转换,失败返回空。
  9. 参数1:转换的数据 (此函数可安全用于二进制对象)
  10. 参数2:原进制  (2进制为1、3进制为2、256进制为255)
  11. 参数3:目标进制
  12. */
  13. function bin_carry_convert($data,$old,$new){
  14. $old &= 255;
  15. $new &= 255;
  16. $k = 0;

  17. $return = array();
  18. $len = strlen($data);
  19. $data_array = array();
  20.   if($old <=0 || $new <= 0 || $len <= 0){
  21.    return;
  22.   }elseif($old == $new) {
  23.    return $data;
  24.    }
  25. $old2 = $old+1;$new2 = $new+1;

  26.   for($i=0;$i<$len;$i++){
  27.    $data_array[$i] = ord($data[$i]);
  28.    if($data_array[$i] > $old){
  29.     return;
  30.    }
  31.   }
  32. while($len >= 1){
  33.   $j = 0;
  34.   for($i=0;$i<$len;$i++){
  35.    $j = $j * $old2 + $data_array[$i];
  36.    $data_array[$i] = floor($j / $new2);
  37.    $j %= $new2;
  38.   }
  39.    $return[$k++] = chr($j);
  40.    $j = 0;
  41.   while($j < $len && $data_array[$j] == 0){
  42.    $j++;
  43.   }
  44.    array_splice($data_array,0,$j);
  45.    $len=sizeof($data_array);
  46.   }
  47. return implode(array_reverse($return));
  48. }

  49. function carry_convert_encode($text){ //进制转换前对字符串进行编码
  50. $len = strlen($text)-1;
  51. if($len <= -1) return;
  52. do {
  53.   $ascii = ord($text[$len]);
  54.    if($ascii >= 48 && $ascii <= 57){
  55.     $ascii -= 48;
  56.    }elseif($ascii >= 65 && $ascii <= 90){
  57.     $ascii -= 55;
  58.    }elseif($ascii >= 97 && $ascii <= 122){
  59.     $ascii -= 61;
  60.    }
  61.   $text[$len] = chr($ascii);
  62. } while($len--);
  63. return $text;
  64. }
  65. function carry_convert_decode($text){ //进制转换后对数据解码成字符串
  66. $len = strlen($text)-1;
  67. if($len <= -1) return;
  68. do {
  69.   $ascii = ord($text[$len]);
  70.    if($ascii >= 0 && $ascii <= 9){
  71.     $ascii += 48;
  72.    }elseif($ascii >= 10 && $ascii <= 35){
  73.     $ascii += 55;
  74.    }elseif($ascii >= 36 && $ascii <= 61){
  75.     $ascii += 61;
  76.    }
  77.   $text[$len] = chr($ascii);
  78. } while($len--);
  79. return $text;
  80. }

  81. function text_carry_convert($text,$old,$new){ //文本2~62进制转换
  82. if($old == $new){
  83.    return $text;
  84. } elseif($old < 2 || $new < 2 || $old > 62 || $new > 62){
  85.   return;
  86. }
  87. return carry_convert_decode(bin_carry_convert(carry_convert_encode($text),$old-1,$new-1));
  88. }
  89. function base_encode($data,$base = 62){ //Base2~62编码程序(失败返回空文本)
  90. if($base < 2 || $base > 62) return;
  91. return carry_convert_decode(bin_carry_convert($data,255,$base-1));
  92. }
  93. function base_decode($text,$base = 62){ //Base2~62解码程序(失败返回空文本)
  94. if($base < 2 || $base > 62) return;
  95. return bin_carry_convert(carry_convert_encode($text),$base-1,255);
  96. }
  97. echo text_carry_convert("2147483647",10,16)."<br>";
  98. echo text_carry_convert("4294967295",10,16)."<br>";
  99. echo text_carry_convert("11111111111111111111111111111111",2,62)."<br>";
  100. echo text_carry_convert("10101010101010101010101010101010",2,62)."<br>";
  101. echo base_encode("Hello World!编码测试",62)."<br>";
  102. echo base_decode("AKRfxYADWZlc0yHqZ9T6Q8mou4C",62)."<br>";
  103. ?>


复制代码


相关帖子

发表于 2019-3-26 18:49:41 | 显示全部楼层
表示看不懂
您需要登录后才可以回帖 登录 | 注册已关闭

本版积分规则

QQ|手机版|Archiver|从F到0 ( 蒙ICP备17002595号-1 )
蒙公网安备15010402000325号

腾讯云安全认证

GMT+8, 2024-4-19 13:13 , Processed in 0.557032 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表