php实现的任意进制互转类分享,php实现进制分享
之前不知道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把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver CS6
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器