本文目錄一覽:
- 1、怎麼用php 里的bin2hex函數算出來的十六進位是「3130」之類的,比如二進位10怎麼算出來就是3130?
- 2、怎樣在PHP中把16進位HEX數據轉換為2進位數據呢?
- 3、求將十進位數轉為十六進位數的JS或PHP代碼
- 4、在PHP中,字母a和字母A的二進位,八進位,十進位,十六進位的寫法是什麼?
- 5、php 怎麼讀取一個文件,保存為16進位的數字。
- 6、PHP里,16進位格式字元串,轉換成具體值的方法
怎麼用php 里的bin2hex函數算出來的十六進位是「3130」之類的,比如二進位10怎麼算出來就是3130?
echo bin2hex(“10”);
輸出3130,確實是這樣的,因為”10″是兩個字元,其ASCII代碼分別是16進位的31和30。
手冊上的內容如下:
bin2hex — 將二進位數據轉換成十六進位表示
說明
string bin2hex ( string $str )
返回 ASCII 字元串,為參數 str 的十六進位表示。轉換使用位元組方式,高四位位元組優先。
怎樣在PHP中把16進位HEX數據轉換為2進位數據呢?
十六進位轉為二進位有兩種理解方法,第一是十六進位字元串轉二進位字元串,第二是十六進位數轉二進位數,下面分別介紹2種轉換方法:
1.hex2bin函數可以將十六進位字元串轉換為二進位字元串,詳細用法如下:
hex2bin — 轉換十六進位字元串為二進位字元串
string hex2bin(string $data)
轉換十六進位字元串為二進位字元串。
參數:
data:十六進位表示的數據
返回值:
返回給定數據的二進位表示 或者在失敗時返回 FALSE。
異常:
如果輸入的十六進位字元串是奇數長數或者無效的十六進位字元串將會拋出E_WARNING 級別的錯誤。
示例:
$hex = hex2bin(“6578616d706c65206865782064617461”);
echo $hex;//example hex data(其中16進位代表的是ascii碼)
2.base_convert是真正意義上的進位轉換函數
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。
參數:
number:要轉換的數字
frombase:原始進位
tobase:轉換後的進位
示例:
$hexadecimal = ‘A37334’;
echo base_convert($hexadecimal, 16, 2);//101000110111001100110100
求將十進位數轉為十六進位數的JS或PHP代碼
JS十進位轉其他進位代碼如下var m = 10;document.write(m.toString(2) + “br”); // 顯示為 1010 二進位document.write(m.toString(8) + “br”); // 顯示為 12 8進位document.write(m.toString(10) + “br”); // 顯示為 10 十進位document.write(m.toString(16) + “br”); // 顯示為 a, 十六進位 php轉換函數如下:bindec() — 二進位轉換為十進位
decbin() — 十進位轉換為二進位
dechex() — 十進位轉換為十六進位
decoct() — 十進位轉換為八進位
hexdec() — 十六進位轉換為十進位
octdec() — 八進位轉換為十進位
base_convert()– 在任意進位之間轉換數字使用說明如下: 一,十進位(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中,字母a和字母A的二進位,八進位,十進位,十六進位的寫法是什麼?
字母”a”:
二進位:1100001
八進位:141
十進位:97
十六進位:61
你可以通過以下代碼來查看:
?php
$num = ord(‘a’);
//二進位
echo decbin($num),’,’;
//八進位
echo decoct($num),’,’;
//十進位
echo $num,’,’;
//十六進位
echo dechex($num),’,’;
php 怎麼讀取一個文件,保存為16進位的數字。
把文件裡面的每一個位元組的ascii碼轉成16進位就可以了,如下:
$content = file_get_contents(“myfile”);
$hex = “”;
for($i=0;$i=strlen($content);$i++){
$asc = ord(substr($content,$i,1));
$hex .= dechex($asc);
}
file_put_contents(“mynewfile”,$hex);
PHP里,16進位格式字元串,轉換成具體值的方法
還有 hexdec 這個函數number hexdec ( string $hex_string )
Example #1 hexdec() 例子
?php
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)”
?
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/243334.html