PHP bin2hex() 函数
PHP bin2hex() 函数用于将 ASCII 字符串转换为十六进制值。
语法:
string bin2hex ( string $str )
参数 | 描述 | 必需/可选 |
---|---|---|
字符串 | 要转换的字符串 | 必需 |
示例1
<?php
str ="Hello World!";
echo "Your ASCII character is: ".str;
echo "<br>"."By using 'bin2hex()' Method your hexadecimal value is: ".bin2hex($str);
?>
输出:
Your ASCII character is:Hello World!
By using 'bin2hex()' Method your hexadecimal value is: 48656c6c6f20576f726c6421
注意:将一个字符串值转换为二进制数再转为十六进制。
示例2
<?php
str = "Hello world!";
echo "Your Hexadecimal Value is ".bin2hex(str) . "<br>";
echo pack("H*",bin2hex($str)) . "<br>";
?>
输出:
Your Hexadecimal Value is 48656c6c6f20776f726c6421
Hello world!
示例3
<?php
binary = "11111001";
echo "Your Binary Value is".binary."<br>";
hex = dechex(bindec(binary));
echo "Converted Binary to Hexadecimal : ".$hex;
?>
输出:
Your Binary Value is11111001
Converted Binary to Hexadecimal : f9