一,十進位(decimal system)轉換函式說明
1,十進位轉二進位 decbin() 函數,如下執行個體
echo decbin(12); //輸出 1100
echo decbin(26); //輸出 11010
decbin
(PHP 3, PHP 4, PHP 5)
decbin -- 十進位轉換為二進位
說明
string decbin ( int number )
返回一字串,包含有給定 number 參數的二進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 32 個 1 的字串。
2,十進位轉八進位 decoct() 函數
echo decoct(15); //輸出 17
echo decoct(264); //輸出 410
decoct
(PHP 3, PHP 4, PHP 5)
decoct -- 十進位轉換為八進位
說明
string decoct ( int number )
返回一字串,包含有給定 number 參數的八進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "37777777777"。
3,十進位轉十六進位 dechex() 函數
echo dechex(10); //輸出 a
echo dechex(47); //輸出 2f
dechex
(PHP 3, PHP 4, PHP 5)
dechex -- 十進位轉換為十六進位
說明
string dechex ( int number )
返回一字串,包含有給定 number 參數的十六進位表示。所能轉換的最大數值為十進位的 4294967295,其結果為 "ffffffff"。
二,二進位(binary system)轉換函式說明
1,二進位轉十六制進 bin2hex() 函數
$binary = "11111001";
$hex = dechex(bindec($binary));
echo $hex;//輸出f9
bin2hex
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
bin2hex -- 將位元據轉換成十六進位表示
說明
string bin2hex ( string str )
返回 ASCII 字串,為參數 str 的十六進位表示。轉換使用位元組方式,高四位位元組優先。
2,二進位轉十制進 bindec() 函數
echo bindec('110011'); //輸出 51
echo bindec('000110011'); //輸出 51
echo bindec('111'); //輸出 7
bindec
(PHP 3, PHP 4, PHP 5)
bindec -- 二進位轉換為十進位
說明
number bindec ( string binary_string )
返回 binary_string 參數所表示的位元的十進位等價值。
bindec() 將一個位元轉換成 integer。可轉換的最大的數為 31 位 1 或者說十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數值,這種情況下,它會返回 float 類型。
三,八進位(octal system)轉換函式說明
八進位轉十進位 octdec() 函數
echo octdec('77'); //輸出 63
echo octdec(decoct(45)); //輸出 45
octdec
(PHP 3, PHP 4, PHP 5)
octdec -- 八進位轉換為十進位
說明
number octdec ( string octal_string )
返回 octal_string 參數所表示的八位元的十進位等值。可轉換的最大的數值為 17777777777 或十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數字,這種情況下,它會返回 float 類型。
四,十六進位(hexadecimal)轉換函式說明
十六進位轉十進位 hexdec()函數
var_dump(hexdec("See"));
var_dump(hexdec("ee"));
// both print "int(238)"
var_dump(hexdec("that")); // print "int(10)"
var_dump(hexdec("a0")); // print "int(160)"
hexdec
(PHP 3, PHP 4, PHP 5)
hexdec -- 十六進位轉換為十進位
說明
number hexdec ( string hex_string )
返回與 hex_string 參數所表示的十六進位數等值的的十進位數。hexdec() 將一個十六進位字串轉換為十進位數。所能轉換的最大數值為 7fffffff,即十進位的 2147483647。PHP 4.1.0 開始,該函數可以處理大數字,這種情況下,它會返回 float 類型。
hexdec() 將遇到的所有非十六進位字元替換成 0。這樣,所有左邊的零都被忽略,但右邊的零會計入值中。
五,任意進位轉換 base_convert() 函數
$hexadecimal = 'A37334';
echo base_convert($hexadecimal, 16, 2);//輸出 101000110111001100110100
base_convert
(PHP 3 >= 3.0.6, PHP 4, PHP 5)
base_convert -- 在任意進位之間轉換數字
說明
string base_convert ( string number, int frombase, int tobase )
返回一字串,包含 number 以 tobase 進位的表示。number 本身的進位由 frombase 指定。frombase 和 tobase 都只能在 2 和 36 之間(包括 2 和 36)。高於十進位的數字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。
這裡主要是把PHP進位轉換函式進行整理,便於開發尋找,相關具體函數說明請參考PHP手冊。請關注下一期中文字元編碼研究系列。