一,十進(jìn)制(decimal system)轉(zhuǎn)換函數(shù)說明
1,十進(jìn)制轉(zhuǎn)二進(jìn)制 decbin() 函數(shù),如下實例
- echo decbin(12); //輸出 1100
- echo decbin(26); //輸出 11010
decbin 十進(jìn)制轉(zhuǎn)換為二進(jìn)制
說明:string decbin ( int number )
返回一字符串,包含有給定 number 參數(shù)的二進(jìn)制表示。所能轉(zhuǎn)換的最大數(shù)值為十進(jìn)制的 4294967295,其結(jié)果為 32 個 1 的字符串。
2,十進(jìn)制轉(zhuǎn)八進(jìn)制 decoct() 函數(shù)
- echo decoct(15); //輸出 17
- echo decoct(264); //輸出 410
decoct -- 十進(jìn)制轉(zhuǎn)換為八進(jìn)制
說明:string decoct ( int number )
返回一字符串,包含有給定 number 參數(shù)的八進(jìn)制表示。所能轉(zhuǎn)換的最大數(shù)值為十進(jìn)制的 4294967295,其結(jié)果為 "37777777777"。
3,十進(jìn)制轉(zhuǎn)十六進(jìn)制 dechex() 函數(shù)
- echo dechex(10); //輸出 a
- echo dechex(47); //輸出 2f
dechex 十進(jìn)制轉(zhuǎn)換為十六進(jìn)制
說明:string dechex ( int number )
返回一字符串,包含有給定 number 參數(shù)的十六進(jìn)制表示。所能轉(zhuǎn)換的最大數(shù)值為十進(jìn)制的 4294967295,其結(jié)果為 "ffffffff"。
二,二進(jìn)制(binary system)轉(zhuǎn)換函數(shù)說明
1,二進(jìn)制轉(zhuǎn)十六制進(jìn) bin2hex() 函數(shù)
- $binary = "11111001";
- $hex = dechex(bindec($binary));
- echo $hex;//輸出f9
bin2hex 將二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成十六進(jìn)制表示
說明:string bin2hex(string str)
返回 ASCII 字符串,為參數(shù) str 的十六進(jìn)制表示。轉(zhuǎn)換使用字節(jié)方式,高四位字節(jié)優(yōu)先。
2,二進(jìn)制轉(zhuǎn)十制進(jìn) bindec() 函數(shù)
- echo bindec('110011'); //輸出 51
- echo bindec('000110011'); //輸出 51
- echo bindec('111'); //輸出 7
bindec 二進(jìn)制轉(zhuǎn)換為十進(jìn)制
說明:number bindec ( string binary_string )
返回 binary_string 參數(shù)所表示的二進(jìn)制數(shù)的十進(jìn)制等價值。
bindec() 將一個二進(jìn)制數(shù)轉(zhuǎn)換成 integer。可轉(zhuǎn)換的最大的數(shù)為 31 位 1 或者說十進(jìn)制的 2147483647。PHP 4.1.0 開始,該函數(shù)可以處理大數(shù)值,這種情況下,它會返回 float 類型。
三,八進(jìn)制(octal system)轉(zhuǎn)換函數(shù)說明
八進(jìn)制轉(zhuǎn)十進(jìn)制 octdec() 函數(shù)
- echo octdec('77'); //輸出 63
- echo octdec(decoct(45)); //輸出 45
octdec 八進(jìn)制轉(zhuǎn)換為十進(jìn)制
說明:number octdec ( string octal_string )
返回 octal_string 參數(shù)所表示的八進(jìn)制數(shù)的十進(jìn)制等值。可轉(zhuǎn)換的最大的數(shù)值為 17777777777 或十進(jìn)制的 2147483647。PHP 4.1.0 開始,該函數(shù)可以處理大數(shù)字,這種情況下,它會返回 float 類型。
四,十六進(jìn)制(hexadecimal)轉(zhuǎn)換函數(shù)說明
十六進(jìn)制轉(zhuǎn)十進(jìn)制 hexdec()函數(shù)
- 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 十六進(jìn)制轉(zhuǎn)換為十進(jìn)制
說明:number hexdec ( string hex_string )
返回與 hex_string 參數(shù)所表示的十六進(jìn)制數(shù)等值的的十進(jìn)制數(shù)。hexdec() 將一個十六進(jìn)制字符串轉(zhuǎn)換為十進(jìn)制數(shù)。所能轉(zhuǎn)換的最大數(shù)值為 7fffffff,即十進(jìn)制的 2147483647。PHP 4.1.0 開始,該函數(shù)可以處理大數(shù)字,這種情況下,它會返回 float 類型。
hexdec() 將遇到的所有非十六進(jìn)制字符替換成 0。這樣,所有左邊的零都被忽略,但右邊的零會計入值中。
五,任意進(jìn)制轉(zhuǎn)換 base_convert() 函數(shù)
- $hexadecimal = 'A37334';
- echo base_convert($hexadecimal, 16, 2);//輸出 101000110111001100110100
base_convert -- 在任意進(jìn)制之間轉(zhuǎn)換數(shù)字
說明:string base_convert ( string number, int frombase, int tobase )
返回一字符串,包含 number 以 tobase 進(jìn)制的表示。number 本身的進(jìn)制由 frombase 指定。frombase 和 tobase 都只能在 2 和 36 之間(包括 2 和 36)。高于十進(jìn)制的數(shù)字用字母 a-z 表示,例如 a 表示 10,b 表示 11 以及 z 表示 35。
新聞熱點
疑難解答