關於PHP中字串與多進位轉換函式的執行個體代碼

來源:互聯網
上載者:User
轉換函式

/** * [字串轉換為(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("%02x", $value);          break;        case 8:          $value = sprintf("%03o", $value);          break;        default:          $value = sprintf("%08b", $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;}

PHP中的多進位

PHP 整型值可以使用十進位,十六進位,八進位或二進位表示,前面可以加上可選的符號(- 或者 +)。

二進位:[+-]?0b[01]+

八進位:[+-]?0[1-7]+

十進位:[+-]?[1-9][0-9]*|0

十六進位:[+-]?[xX][0-9a-fA-F]+

多進位轉換函式:

以上就是小編為大家帶來的關於PHP中字串與多進位轉換函式的執行個體代碼全部內容了,希望大家多多支援topic.alibabacloud.com~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.