PHP进制转换实例分析(2,8,16,36,64进制至10进制相互转换)

作者:袖梨 2022-06-24

本文实例讲述了PHP进制转换。分享给大家供大家参考,具体如下:

可以实现:

10进制转换2、8、16、36、62进制

2、8、16、36、62进制转换10进制

有点要注意下,2、8、16进制转换时,使用的是系统的自己的函数。

所以,不管怎么高精度转换值可能大于2147483646。

另外,

32进制低精转换,最大值:2147483646;
32进制高精转换,最大值:77309411327;
64进制高精转换,最大值:133143986175。

jinzhi.php文件,自带演示功能。

 代码如下 复制代码

$mtime1=explode(" ", microtime());

$startTime=$mtime1[0] +$mtime1[1];

?>

进制转换/title></p><p></head></p><p><body></p><p><form id="jzh"name="jinzhih"method="post"action=""></p><p><input name="go"type="hidden"value="100"></p><p><table width="482"border="1"cellpadding="0"cellspacing="0"bordercolor="#FFE8E8"bgcolor="#F4F8FB"></p><p> <tr></p><p>  <td width="194"><select name="jinzhi0"id="jinzhi0"></p><p>  <option value="2">2</option></p><p>  <option value="8">8</option></p><p>  <option value="10"selected="selected">10</option></p><p>  <option value="16">16</option></p><p>  <option value="36">36</option></p><p>  <option value="62">62</option></p><p>  </select></p><p>进制</td></p><p> <td width="275"><input name="zhi"type="text"id="zhi"/></td></p><p> </tr></p><p> <tr></p><p>  <td>进行<input name="lx"type="radio"value="0"checked="checked"/></p><p>普通<input type="radio"name="lx"value="1"/>高精</td><td> </td></p><p> </tr></p><p> <tr></p><p>  <td>转换为</p><p>  <select name="jinzhi1"id="jinzhi1"></p><p>   <option value="2"selected="selected">2</option></p><p>   <option value="8">8</option></p><p>   <option value="10">10</option></p><p>   <option value="16">16</option></p><p>   <option value="36">36</option></p><p>   <option value="62">62</option></p><p>  </select>进制,</td><td>总长度为</p><p>  <input name="changdu"type="text"id="changdu"value="10"size="4"maxlength="2"/></p><p>字符。</td></tr><tr></p><p> <td><input type="submit"name="Submit"value="提交"/></td></p><p> <td><input type="reset"name="Submit2"value="重置"/></td></p><p> </tr></table></p><p> <p>注意:仅能进行10进制转换为2、8、16、36、62进制;或反转换。</p></form></p><p><?php</p><p>classjinzhi_class</p><p>{</p><p>   //10进制转2、8、16、36、62进制</p><p>   functionjinzhih_0($shu,$jinzhi,$w)</p><p>   {</p><p>   $zifu="";</p><p>   while($shu!=0){</p><p>   $linshi=$shu%$jinzhi;</p><p>   switch($jinzhi){</p><p>    case2:</p><p>     $zifu=decbin($shu);</p><p>     return$zifu;</p><p>    case8:</p><p>     $zifu=decoct($shu);</p><p>     return$zifu;</p><p>    case16:</p><p>     $zifu=dechex($shu);</p><p>     return$zifu;</p><p>    case36:</p><p>     if($linshi>=10)</p><p>     {</p><p>     $zifu.=chr(($linshi+55));</p><p>     }else{</p><p>      $zifu.=$linshi;}</p><p>     break;</p><p>    case62:</p><p>     if(($linshi>=10) && ($linshi36)) {$zifu.=chr($linshi+55);break;}</p><p>     if(($linshi>=36) && ($linshi62)) {$zifu.=chr($linshi+61);break;}</p><p>     $zifu.=$linshi;break;</p><p>   default:</p><p>   $zifu.=$linshi;</p><p>   break;</p><p>   }</p><p>   $shu=intval($shu/$jinzhi);</p><p>   }</p><p>   for($i=strlen($zifu);$i$w;$i++)</p><p>   $zifu.="0";</p><p>  returnstrrev($zifu);}</p><p>   //2、8、16、36、62进制转10进制</p><p>   functionjinzhih_1($zifu,$jinzhi,$w)</p><p> {$shu=0;</p><p>  for($i=0;$i=strlen($zifu)-1;$i++)</p><p>     {</p><p>     $linshi=substr($zifu,$i,1);</p><p>     switch($jinzhi){</p><p>      case2:</p><p>       $shu=bindec($zifu);</p><p>       $i=strlen($zifu)+1;</p><p>       break;</p><p>      case8:</p><p>       $shu= octdec($zifu);</p><p>      $i=strlen($zifu)+1;</p><p>      break;</p><p>      case16:</p><p>       $shu= hexdec($zifu);</p><p>       $i=strlen($zifu)+1;</p><p>       break;</p><p>     case36:</p><p>      if(ord($linshi)=57)</p><p>      {$shu+=(ord($linshi)-48)*pow($jinzhi,strlen($zifu)-$i-1);</p><p>     }else{</p><p>       $shu=$shu+ (ord($linshi)-55)*pow($jinzhi,strlen($zifu)-$i-1);}</p><p>       break;</p><p>     case62:</p><p>      if(ord($linshi)=57)</p><p>      {$shu+=$linshi*pow($jinzhi,strlen($zifu)-$i-1);    </p><p>     }elseif((ord($linshi)>=65) && (ord($linshi)=90)){</p><p>      $shu+= (ord($linshi)-55)*pow($jinzhi,strlen($zifu)-$i-1);</p><p>      }else{</p><p>      $shu+= (ord($linshi)-61)*pow($jinzhi,strlen($zifu)-$i-1);}</p><p>      break;</p><p>     }</p><p>   }</p><p>   return$shu;</p><p>   }</p><p>   //10进制高精度转换2、8、16、36、62进制</p><p>   functionjinzhih_G0($shu,$jinzhi,$w)</p><p>   {</p><p>   $zifu="";</p><p>   while($shu!=0){</p><p>   $linshi=bcmod($shu,$jinzhi);</p><p>   switch($jinzhi){</p><p>    case2:</p><p>     $zifu=decbin($shu);</p><p>     return$zifu;</p><p>    case8:</p><p>     $zifu=decoct($shu);</p><p>     return$zifu;</p><p>    case16:</p><p>     $zifu=dechex($shu);</p><p>     return$zifu;</p><p>    case36:</p><p>     if($linshi>=10)</p><p>     {</p><p>     $zifu.=chr(($linshi+55));</p><p>     }else{</p><p>      $zifu.=$linshi;}</p><p>     break;</p><p>    case62:</p><p>     if(($linshi>=10) && ($linshi36)) {$zifu.=chr($linshi+55);break;}</p><p>     if(($linshi>=36) && ($linshi62)) {$zifu.=chr($linshi+61);break;}</p><p>     $zifu.=$linshi;break;</p><p>   default:</p><p>   $zifu.=$linshi;</p><p>   break;</p><p>   }</p><p>   $shu=intval(bcdiv($shu,$jinzhi));</p><p>   }</p><p>   for($i=strlen($zifu);$i$w;$i++)</p><p>   $zifu.="0";</p><p>  returnstrrev($zifu);}</p><p>   //2、8、16、36、62进制高精度转换10进制</p><p>   functionjinzhih_G1($zifu,$jinzhi,$w)</p><p> {$shu="";</p><p>  for($i=0;$i=strlen($zifu)-1;$i++)</p><p>     {</p><p>     $linshi=substr($zifu,$i,1);</p><p>     switch($jinzhi){</p><p>      case2:</p><p>       $shu=bindec($zifu);</p><p>       $i=strlen($zifu)+1;</p><p>       break;</p><p>      case8:</p><p>       $shu= octdec($zifu);</p><p>      $i=strlen($zifu)+1;</p><p>      break;</p><p>      case16:</p><p>       $shu= hexdec($zifu);</p><p>       $i=strlen($zifu)+1;</p><p>       break;</p><p>     case36:</p><p>      if(ord($linshi)=57)</p><p>      {$shu=bcadd($shu,bcmul((ord($linshi)-48),bcpow($jinzhi,strlen($zifu)-$i-1)));</p><p>     }else{</p><p>       $shu=bcadd($shu,bcmul((ord($linshi)-55),bcpow($jinzhi,strlen($zifu)-$i-1)));}</p><p>       break;</p><p>     case62:</p><p>      if(ord($linshi)=57)</p><p>      {$shu=bcadd($shu,bcmul($linshi,bcpow($jinzhi,strlen($zifu)-$i-1)));</p><p>     }elseif((ord($linshi)>=65) && (ord($linshi)=90)){</p><p>      $shu=bcadd($shu,bcmul((ord($linshi)-55),bcpow($jinzhi,strlen($zifu)-$i-1)));</p><p>      }else{</p><p>      $shu=bcadd($shu,bcmul((ord($linshi)-61),bcpow($jinzhi,strlen($zifu)-$i-1)));</p><p>      }</p><p>      break;</p><p>     }</p><p>   }</p><p>   return$shu;}</p><p>}</p><p>if(isset($_GET["p"]))</p><p>{if($_GET["p"]==="1"){</p><p>echo"";</p><p>echo"提示:</p><p>";</p><p>echo"32进制低精转换,最大值:2147483646</p><p>";</p><p>echo"32进制高精转换,最大值:77309411327</p><p>";</p><p>echo"64进制高精转换,最大值:133143986175</p><p>";</p><p>echo"";</p><p>echo"值为:";</p><p>$Fs=newjinzhi_class();</p><p>if($_POST['lx']=="0"&&$_POST['jinzhi0']=="10")</p><p>{echo$Fs->jinzhih_0($_POST['zhi'],$_POST['jinzhi1'],$_POST['changdu'])."</p><p>";}</p><p>if($_POST['lx']=="1"&&$_POST['jinzhi0']=="10")</p><p>{echo$Fs->jinzhih_G0($_POST['zhi'],$_POST['jinzhi1'],$_POST['changdu']);}</p><p>if($_POST['lx']=="0"&&$_POST['jinzhi0']>"10")</p><p>{echo$Fs->jinzhih_1($_POST['zhi'],$_POST['jinzhi0'],$_POST['changdu']);}</p><p>if($_POST['lx']=="1"&&$_POST['jinzhi0']>"10")</p><p>{echo$Fs->jinzhih_G1($_POST['zhi'],$_POST['jinzhi0'],$_POST['changdu']);}</p><p>}</p><p>echo"";}</p><p>$mtime1=explode(" ", microtime());</p><p>$endTime=$mtime1[0] +$mtime1[1];</p><p>printf ("页面执行时间:%.6fs.",$endTime-$startTime);</p><p>?></p></td></tr></table> </div> </div> </section> <section class="wrap-box"> <div class="g-tit"> <h2>相关文章</h2> </div> <ul class="s-list nobord notop"> <li> <a href="/art-422484.htm" class="s-card"> <div class="s-card-l"> <p class="tit">“十月朝,糍粑碌碌烧”说的是小雪时节的哪一项习俗 蚂蚁庄园11月22日答案早知道</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67445b7e892e41.png" alt="“十月朝,糍粑碌碌烧”说的是小雪时节的哪一项习俗 蚂蚁庄园11月22日答案早知道" /> </div> </a> </li> <li> <a href="/art-422483.htm" class="s-card"> <div class="s-card-l"> <p class="tit">以闪亮之名宠物礼包怎么样</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67444aa6a570b1.jpeg" alt="以闪亮之名宠物礼包怎么样" /> </div> </a> </li> <li> <a href="/art-422482.htm" class="s-card"> <div class="s-card-l"> <p class="tit">崩坏星穹铁道星期日用什么光锥</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_67444aa5cd6c41.jpeg" alt="崩坏星穹铁道星期日用什么光锥" /> </div> </a> </li> <li> <a href="/art-422480.htm" class="s-card"> <div class="s-card-l"> <p class="tit">崩坏星穹铁道星期日用什么光锥 崩铁星期日光锥推荐搭配介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_6744442f826b91.jpeg" alt="崩坏星穹铁道星期日用什么光锥 崩铁星期日光锥推荐搭配介绍" /> </div> </a> </li> <li> <a href="/art-422481.htm" class="s-card"> <div class="s-card-l"> <p class="tit">崩坏星穹铁道星期日技能机制怎么样 崩铁星期日技能机制介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_674444301a6b21.jpeg" alt="崩坏星穹铁道星期日技能机制怎么样 崩铁星期日技能机制介绍" /> </div> </a> </li> <li> <a href="/art-422479.htm" class="s-card"> <div class="s-card-l"> <p class="tit">崩坏星穹铁道星期日遗器怎么选择 崩铁星期日遗器推荐搭配介绍</p> <div class="info"> <span class="person">游戏攻略</span> <span class="time">2024-11-25</span> </div> </div> <div class="s-card-pic"> <img src="/images/lazy.gif" data-src="/uploads/20241125/logo_6744442e529711.jpeg" alt="崩坏星穹铁道星期日遗器怎么选择 崩铁星期日遗器推荐搭配介绍" /> </div> </a> </li> </ul> </section> <section class="wrap-box"> <div class="g-tit"> <h2>精彩推荐</h2> </div> <ul class="card-box"> <li class="card3"> <a href="/app/99428.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/671258fc7c56f.png" alt="沙威玛传奇安卓版" /> </div> <p class="figure-head">沙威玛传奇安卓版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99427.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711fc29100e3.png" alt="战盟大乱斗最新版" /> </div> <p class="figure-head">战盟大乱斗最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99426.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ef93e4d90.png" alt="大侠立志传最新版" /> </div> <p class="figure-head">大侠立志传最新版</p> <span class="figure-btn">下载</span> </a> </li> <li class="card3"> <a href="/app/99425.htm" target="_self" class="figure"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241018/6711ca5ba8572.png" alt="苍翼混沌效应最新版" /> </div> <p class="figure-head">苍翼混沌效应最新版</p> <span class="figure-btn">下载</span> </a> </li> </ul> <ul class="card-box-b"> <li class="card10"> <a href="/app/99454.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241019/671393f16002e.jpg" alt="驯龙高手旅程安卓版" /> </div> <div class="figure-cont"> <p class="figure-head">驯龙高手旅程安卓版</p> <div class="figure-desc"> <span>模拟经营</span> <span>驯龙高手旅程安卓版</span> </div> <div class="figure-desc"> <p>一款以奇幻世界为背景的模拟经营游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99397.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20241015/670dc626d93dd.png" alt="全民农场最新版" /> </div> <div class="figure-cont"> <p class="figure-head">全民农场最新版</p> <div class="figure-desc"> <span>模拟经营</span> <span>全民农场最新版</span> </div> <div class="figure-desc"> <p>一款轻松风格的农场经营类型的游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99010.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc0ba5ab7021.jpg" alt="散修功德模拟器完整版" /> </div> <div class="figure-cont"> <p class="figure-head">散修功德模拟器完整版</p> <div class="figure-desc"> <span>模拟经营</span> <span>散修功德模拟器完整版</span> </div> <div class="figure-desc"> <p>散修功德模拟器游戏是一款以功德修炼为题材的模拟游戏</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/99001.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dc094ae30941.jpg" alt="我的毕业日记无限金币下载" /> </div> <div class="figure-cont"> <p class="figure-head">我的毕业日记无限金币下载</p> <div class="figure-desc"> <span>模拟经营</span> <span>我的毕业日记无限金币下载</span> </div> <div class="figure-desc"> <p>开始毕业后的新生活吧!</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> <li class="card10"> <a href="/app/98996.htm" target="_self" class="figure2"> <div class="figure-box"> <img src="/images/lazy.gif" data-src="https://img.111cn.net/uploads/20240907/logo_66dbd35fadafd1.png" alt="城堡之梦拼图和装饰下载最新版" /> </div> <div class="figure-cont"> <p class="figure-head">城堡之梦拼图和装饰下载最新版</p> <div class="figure-desc"> <span>模拟经营</span> <span>城堡之梦拼图和装饰下载最新版</span> </div> <div class="figure-desc"> <p>城堡之梦拼图和装饰玩家将会在游戏当中感受更多的乐趣体验,你可</p> </div> </div> <span class="figure-btn">下载</span> </a> </li> </ul> </section> <footer class="foot"> <a href="/" class="logo-icon"> <img src="/mobile/images/logo2.png" alt="一聚教程网"> </a> <p>Copyright © 2010-2022</p> <p>111cn.net All Rights Reserved</p> </footer> <script> var advData = {"img_fixed_pc_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e9729f58.jpg","img_fixed_mob_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e8793225.jpg","url_adv":"http:\/\/shop.hushen.cn\/shop\/c\/baojianpin.html","str_adv":"\u864e\u795e\u5546\u57ce\uff1a\u5173\u7231\u7537\u6027\uff0c\u66f4\u61c2\u7537\u4eba\u3002\u89e3\u51b3\u5927\u4f17\u7684\u7537\u8a00\u4e4b\u9690","img_popup_adv":"https:\/\/img.111cn.net\/uploads\/20240509\/663c2e748238d.png","pc_show_img":"2","pc_show_popup":"2","pc_show_video":"2","mob_show_img":"2","mob_show_popup":"2","mob_show_video":"2","close_adv":"https:\/\/img.111cn.net\/uploads\/20240508\/663b20650801e.png","video_adv":"\/pc\/images\/pc-adv.mp4"}; </script> <script src="/jspc/funcmob.js" type="text/javascript"></script> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-DSRRGRV1TL"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-DSRRGRV1TL'); </script> <div class="back-top" style="display: block;"> <span class="icon-box"> <svg class="icon" viewBox="0 0 1024 1024"> <path d="M213.333333 640h170.666667v256h256v-256h170.666667l-298.666667-341.333333zM170.666667 128h682.666666v85.333333H170.666667z" fill="#0374f3"></path> </svg> </span> </div> </div> <script src="/js/stat.js"></script> </body> </html>