php实现的任意进制互转类分享


Posted in PHP onJuly 07, 2015

之前不知道php自带有base_convert可以实现任意进制之间的转换,自己写了一个。。。。

<?php 
/**
 * 进制转换类 
 * @author sgf@funcity
 * @version 2011-02-15
 */
Class Hex{
 
 private static $element = array(
 '0','1','2','3','4','5','6','7','8','9',
 'A','B','C','D','E','F','G','H','I','J',
 'K','L','M','N','O','P','Q','R','S','T',
 'U','V','W','X','Y','Z'
 );
 
 private static $hex_min = 2;
 private static $hex_max = 36;
 
 
 /**
 * 进制转换
 */
 public function conv($int,$out_hex,$in_hex=10,$use_system=true){
 
 if($use_system && function_exists('base_convert')){
  return strtoupper(base_convert($int,$in_hex,$out_hex));
 }
 
 if($out_hex == $in_hex){
  return $int;
 }
 if($out_hex > self::$hex_max || $out_hex < self::$hex_min){
  return false;
 }
 if($in_hex > self::$hex_max || $in_hex < self::$hex_min){
  return false;
 }
 $hex_10 = $this->_conv2hex10($int,$in_hex);
 return strtoupper($this->_conv_hex($hex_10,$out_hex));
 }
 
 /**
 * 将任意进制数字转为10进制数字
 */
 private function _conv2hex10($int,$in_hex){
 $int = strtoupper(trim($int));
 if($in_hex==10){
  return $int;
 }elseif( $in_hex== 2 && function_exists('bindec')){
  return bindec($int);
 } elseif($in_hex== 16 && function_exists('hexdec')){
  return hexdec($int);
 } elseif($in_hex== 8 && function_exists('octdec')){
  return octdec($int);
 }
 $array = array();
 $result = 0;
 for($i=0;$i<strlen($int);$i++){
  array_unshift( $array, substr($int,$i,1)); //插入到数组头部(既倒序)
 }
 foreach($array as $k => $v){
 
  $hex10_value = array_search($v,self::$element);
  if($hex10_value==-1){
  return false;
  }
  $result += intval( pow($in_hex,$k) * $hex10_value );
 
 }
 return $result;
 }
 
 /**
 * 把10进制数换成任意进制数
 */
 private function _conv_hex($hex_10,$out_hex){
 
 $hex_10 = intval($hex_10);
 
 if($out_hex==10){
  return $hex_10;
 }else if( $out_hex==2  && function_exists('decbin')){
  return decbin($hex_10);
 } elseif ( $out_hex ==16 && function_exists('dechex')){
  return dechex($hex_10);
 } elseif ( $out_hex ==8 && function_exists('decoct')){
  return decoct($hex_10);
 }
 
 $array = array();
 $result = ""; 
 
 //利用10进制数除任意进制数 倒取余数法转换。
 do {
  array_unshift( $array, $hex_10 % $out_hex); //余数插入到数组数组第1个位置。
  $hex_10 = $hex_10 / $out_hex ; //除法
 } while ($hex_10>1);
 
 foreach($array as $k){
  $result .= self::$element[$k]; 
 }
 return $result; 
 }
 
 
}
?>
PHP 相关文章推荐
PHP4实际应用经验篇(9)
Oct 09 PHP
PHP新手上路(十)
Oct 09 PHP
PHP代码网站如何防范SQL注入漏洞攻击建议分享
Mar 01 PHP
有关PHP中MVC的开发经验分享
May 17 PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
Nov 26 PHP
推荐一款MAC OS X 下php集成开发环境mamp
Nov 08 PHP
php限制ip地址范围的方法
Mar 31 PHP
php使用Jpgraph绘制复杂X-Y坐标图的方法
Jun 10 PHP
PHP获取一年有几周以及每周开始日期和结束日期
Aug 06 PHP
php项目开发中用到的快速排序算法分析
Jun 25 PHP
PHP判断是否是微信打开还是浏览器打开的方法
Feb 27 PHP
PHP Swoole异步Redis客户端实现方法示例
Oct 24 PHP
Apache服务器下防止图片盗链的办法
Jul 06 #PHP
php获取指定(访客)IP所有信息(地址、邮政编码、国家、经纬度等)的方法
Jul 06 #PHP
php实现搜索一维数组元素并删除二维数组对应元素的方法
Jul 06 #PHP
jquery+php实现导出datatables插件数据到excel的方法
Jul 06 #PHP
php导出中文内容excel文件类实例
Jul 06 #PHP
PHP伪造来源HTTP_REFERER的方法实例详解
Jul 06 #PHP
PHP 错误处理机制
Jul 06 #PHP
You might like
PHP4和PHP5性能测试和对比 测试代码与环境
2007/08/17 PHP
PHP中auto_prepend_file与auto_append_file用法实例分析
2014/09/22 PHP
ThinkPHP中处理表单中的注意事项
2014/11/22 PHP
php微信公众号开发之简答题
2018/10/20 PHP
jquery文字上下滚动的实现方法
2013/03/22 Javascript
JavaScript获得当前网页来源页面(即上一页)的方法
2015/04/03 Javascript
在JavaScript中使用NaN值的方法
2015/06/05 Javascript
jquery实现简单的自动播放幻灯片效果
2015/06/13 Javascript
jQuery+CSS实现简单切换菜单示例
2016/07/27 Javascript
Angular+Node生成随机数的方法
2017/06/16 Javascript
jQuery轮播图实例详解
2018/08/15 jQuery
layui表格 列自动适应大小失效的解决方法
2019/09/06 Javascript
JS实现吸顶特效
2020/01/08 Javascript
vue使用screenfull插件实现全屏功能
2020/09/17 Javascript
[34:56]Ti4冒泡赛LGD vs Liquid 1
2014/07/14 DOTA
[01:00]DOTA2 store: Collection of Artisan's Wonders
2015/08/12 DOTA
[03:01]完美盛典趣味短片 DOTA2年度最佳&拉胯英雄
2019/12/07 DOTA
Python新手实现2048小游戏
2015/03/31 Python
在Python中使用lambda高效操作列表的教程
2015/04/24 Python
python 2.7 检测一个网页是否能正常访问的方法
2018/12/26 Python
ZABBIX3.2使用python脚本实现监控报表的方法
2019/07/02 Python
pybind11在Windows下的使用教程
2019/07/04 Python
jupyter 实现notebook中显示完整的行和列
2020/04/09 Python
PyQt5连接MySQL及QMYSQL driver not loaded错误解决
2020/04/29 Python
python 监控logcat关键字功能
2020/09/04 Python
详解基于python的图像Gabor变换及特征提取
2020/10/26 Python
Sephora丝芙兰泰国官方网站:国际知名化妆品购物
2017/11/15 全球购物
Hotels.com日本:国外和海外住宿,酒店预订
2019/12/13 全球购物
高三家长寄语
2014/04/03 职场文书
《池塘边的叫声》教学反思
2014/04/12 职场文书
拓展训练激励口号
2014/06/17 职场文书
党支部考察意见范文
2015/06/02 职场文书
信用卡工作证明范本
2015/06/19 职场文书
2016年教师节贺卡寄语
2015/12/04 职场文书
Mysql基础之常见函数
2021/04/22 MySQL
浅谈Redis变慢的原因及排查方法
2022/06/21 Redis