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 相关文章推荐
我常用的几个类
Oct 09 PHP
建立文件交换功能的脚本(二)
Oct 09 PHP
php中计算程序运行时间的类代码
Nov 03 PHP
Function eregi is deprecated (解决方法)
Jun 21 PHP
WordPress中访客登陆实现邮件提醒的PHP脚本实例分享
Dec 14 PHP
Smarty实现页面静态化(生成HTML)的方法
May 23 PHP
PHP上传图片时判断上传文件是否为可用图片的方法
Oct 20 PHP
PHP Header用于页面跳转时的几个注意事项
Oct 21 PHP
php实现和c#一致的DES加密解密实例
Jul 24 PHP
Laravel框架中Blade模板的用法示例
Aug 30 PHP
PHP实现数据库统计时间戳按天分组输出数据的方法
Oct 10 PHP
PHP+Redis事务解决高并发下商品超卖问题(推荐)
Aug 03 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
四月新番又没了,《Re:从零开始的异世界生活》第二季延期至7月播出
2020/05/06 日漫
十天学会php之第九天
2006/10/09 PHP
php mysql索引问题
2008/06/07 PHP
php获取网页内容方法总结
2008/12/04 PHP
php绘制圆形的方法
2015/01/24 PHP
Laravel 5框架学习之表单验证
2015/04/08 PHP
WordPress中调试缩略图的相关PHP函数使用解析
2016/01/07 PHP
php+mysql实现的二级联动菜单效果详解
2016/05/10 PHP
PHP中利用sleep函数实现定时执行功能实现代码
2016/08/25 PHP
PHP htmlentities()函数用法讲解
2019/02/25 PHP
JavaScript 创建运动框架的实现代码
2013/05/08 Javascript
JS建造者模式基本用法实例分析
2015/06/30 Javascript
javascript常用函数(2)
2015/11/05 Javascript
JS中call/apply、arguments、undefined/null方法详解
2016/02/15 Javascript
详解Angular.js数据绑定时自动转义html标签及内容
2017/03/30 Javascript
JS将unicode码转中文方法
2017/05/08 Javascript
让微信小程序支持ES6中Promise特性的方法详解
2017/06/13 Javascript
详解vue中组件参数
2018/07/09 Javascript
跟老齐学Python之复习if语句
2014/10/02 Python
python在windows和linux下获得本机本地ip地址方法小结
2015/03/20 Python
详解Python如何生成词云的方法
2018/06/01 Python
对TensorFlow的assign赋值用法详解
2018/07/30 Python
python重试装饰器的简单实现方法
2019/01/31 Python
用python做游戏的细节详解
2019/06/25 Python
python使用sklearn实现决策树的方法示例
2019/09/12 Python
CSS3 边框效果
2019/11/04 HTML / CSS
当文件系统受到破坏时,如何检查和修复系统?
2012/03/09 面试题
自荐信不宜过于夸大
2013/11/06 职场文书
办公室保洁员岗位职责
2013/12/02 职场文书
市场开发与营销专业求职信范文
2014/05/01 职场文书
群众路线剖析材料
2014/09/30 职场文书
谢师宴学生答谢词
2015/09/30 职场文书
css3实现的加载动画效果
2021/04/07 HTML / CSS
世界十大儿童漫画书排名,法国国宝漫画排第五,第二是轰动日本连环
2022/03/18 欧美动漫
Python基本知识点总结
2022/04/07 Python
Nginx的gzip相关介绍
2022/05/11 Servers