源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

关于PHP中字符串与多进制转换函数的实例代码

  • 时间:2020-06-25 17:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:关于PHP中字符串与多进制转换函数的实例代码
[b]转换函数[/b]
/**
 * [字符串转换为(2,8,16进制)ASCII码]
 * @param string $str   [待处理字符串]
 * @param boolean $encode [字符串转换为ASCII|ASCII转换为字符串]
 * @param string $intType [2,8,16进制标示]
 * @return string byte_str [处理结果]
 * @author alexander
 */
function strtoascii($str, $encode=true, $intType="2"){
  if($encode == true){
    $byte_array = str_split($str);
    foreach($byte_array as &$value){
      $value = ord($value);
      switch ($intType) {
        case 16:
          $value = sprintf("x", $value);
          break;
        case 8:
          $value = sprintf("o", $value);
          break;
        default:
          $value = sprintf("b", $value);
          break;
      }
    }
    unset($value);
    $byte_str = implode('', $byte_array);
  }
  else{
    $chunk_size = $intType == 16 ? 2 : ($intType == 8 ? 3 : 8);
    $byte_array = chunk_split($str, $chunk_size);
    $byte_array = array_filter(explode("\r\n", $byte_array));
    foreach($byte_array as &$value){
      $fun_name = $intType == 16 ? 'hexdec' : ($intType == 8 ? 'octdec' : 'bindec');
      $value = $fun_name($value);
      $value = chr($value);
    }
    unset($value);
    $byte_str = implode('', $byte_array);
  }
  return $byte_str;
}
[b]PHP中的多进制[/b] PHP 整型值可以使用十进制,十六进制,八进制或二进制表示,前面可以加上可选的符号(- 或者 +)。 二进制:[+-]?0b[01]+ 八进制:[+-]?0[1-7]+ 十进制:[+-]?[1-9][0-9]*|0 十六进制:[+-]?[xX][0-9a-fA-F]+ [b]多进制转换函数:[/b]  
[url=http://php.net/manual/zh/function.bindec.php]bindec[/url] 二进制转换为十进制
[url=http://php.net/manual/zh/function.decbin.php]decbin[/url] 十进制转换为二进制
[url=http://php.net/manual/zh/function.octdec.php]octdec[/url] 八进制转换为十进制
[url=http://php.net/manual/zh/function.decoct.php]decoct[/url] 十进制转换为八进制
[url=http://php.net/manual/zh/function.hexdec.php]hexdec[/url] 十六进制转换为十进制
[url=http://php.net/manual/zh/function.dechex.php]dechex[/url] 十进制转换为十六进制
以上就是小编为大家带来的关于PHP中字符串与多进制转换函数的实例代码全部内容了,希望大家多多支持编程素材网~
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部