> php教程 > php手册 > PHP整数N进制转换,短网址ID转换

PHP整数N进制转换,短网址ID转换

WBOY
풀어 주다: 2016-06-07 11:45:09
원래의
1429명이 탐색했습니다.

功能: 10进制转换n进制
n进制转换10进制
n可为2、8、10、32、62
<?php <br /> /*<br> * ===========================================<br> * 进制转换<br> * 功能: 10进制转换n进制<br>         n进制转换10进制<br> * ===========================================<br> */<br> class IntSwitch{<br> <br>     public function __construct(){<br>         $this->key = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";  //默认密钥<br>         $this->carry = 1;   //默认进位<br>     }<br> <br>     //普通整数N进制转换,将$raw转换为$ary,文本长度为$len<br>     public function changeInt($raw,$ary,$len){<br>         //变量初始化<br>         $result = "";       //结果<br>         $variable = 1;      //临时变量<br>         $residue = 1;       //余数<br>         $median = 1;        //文本长度<br>         $verify = $raw;     //原数值      <br>         if ($raw == 0) <br>             $result = substr($this->key,0,$this->carry);<br>         while($raw != 0){<br>             $variable = intval($raw / $ary);<br>             $residue = $raw % $ary;<br>             $result = substr($this->key,$residue*$this->carry,$this->carry).$result;<br>             $raw = $variable;<br>         }<br>         $median = strlen($result);//取结果文本长度<br>         if ($median             $result = $this->fillPlace($len-$median).$result;<br>         if ($this->revertInt($ary,$result)!=$verify)<br>             return -1;<br>         return $result;<br> <br>     }<br> <br>     //普通整数N进制反转换<br>     public function revertInt($ary,$value){<br>         //变量初始化<br>         $result = "";<br>         $median = intval(strlen($value)/$this->carry);<br>         $character = "";        <br>         for ($i=1;$i             if ($this->carry>1){//多进位进制转换                <br>                 $character = substr($value,$i*$this->carry-($this->carry),$this->carry);                <br>                 $result += (intval(strpos($this->key,$character)/$this->carry))*pow($ary,$median - $i);<br>             }else{//单进位进制转换                <br>                 $character = substr($value,$i*$this->carry-1,$this->carry);<br>                 $result += intval(strpos($this->key,$character))*pow($ary,$median - $i);<br>             }<br>         }<br>         return $result;<br>     }<br> <br>     //大整数N进制转换,将$raw转换为$ary,文本长度为$len<br>     public function changeBigInt($raw,$ary,$len){<br>         //变量初始化<br>         bcscale(0);           //设置没有小数位。<br>         $result = "";         //结果<br>         $variable = 1;        //临时变量<br>         $residue = 1;         //余数<br>         $median = 1;          //文本长度<br>         $verify = $raw;       //原数值<br>         if ($raw == "0") <br>             $result = substr($this->key,0,$this->carry);<br>         while($raw != "0"){<br>             $variable = bcdiv($raw,$ary);<br>             $residue = bcmod($raw,$ary);<br>             $result = substr($this->key,$residue*$this->carry,$this->carry).$result;<br>             $raw = $variable;<br>         }<br>         $median = strlen($result);//取结果文本长度<br>         if ($median             $result = $this->fillPlace($len-$median).$result;<br>         if ($this->revertBigInt($ary,$result)!=$verify)<br>             return -1;<br>         return $result;<br> <br>     }<br> <br>     //大整数N进制反转换<br>     public function revertBigInt($ary,$value){<br>         //变量初始化<br>         bcscale(0);         //设置没有小数位。<br>         $result = "";<br>         $median = bcdiv(strlen($value),$this->carry);<br>         $character = "";        <br>         for ($i=1;$i             if ($this->carry>1){//多进位进制转换                <br>                 $character = substr($value,$i*$this->carry-($this->carry),$this->carry);    <br>                 $result = bcadd(bcmul(bcdiv(strpos($this->key,$character),$this->carry),bcpow($ary,$median - $i)),$result);<br>             }else{//单进位进制转换                <br>                 $character = substr($value,$i*$this->carry-1,$this->carry);<br>                 $result = bcadd(bcmul(strpos($this->key,$character),bcpow($ary,$median - $i)),$result);<br>             }<br>         }<br>         return $result;<br>     }<br> <br>     //补位函数<br>     public function fillPlace($number){<br>         $character = substr($this->key,0,$this->carry);//取默认为0的字符。<br>         $result = $character;<br>         for ($i=1;$i             $result.=$character;    <br>         return $result;<br>     }<br> }<br> <br> $raw = "19999999999";//数据<br> $ary = 62;//进制<br> $len = 6;//位数,0为自动<br> $is = new IntSwitch;<br> $result = $is->changeBigInt($raw,$ary,$len);<br> $revert=$is->revertBigInt($ary,$result);<br> echo "<font><b>$raw </b></font> 转换为 $ary 进制 = <font><b>$result </b></font><br>";<br> echo "实际长度:".strlen($result)."<br>";<br> echo "还原数据:".$revert."<br>";

附件 IntSwitch.class.zip ( 1.2 KB 下载:211 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿