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

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

WBOY
Lepaskan: 2016-06-07 11:45:09
asal
1428 orang telah melayarinya

功能: 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元

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan