Home > Article > Backend Development > PHP bin2hex()
PHP bin2hex() is a function which is used to convert any string to hexadecimal values. The binary string conversion is traversed through byte-wise with continuous conversions happening at the background. One very important aspect of this function is that bin2hex() does not represents that the string will represent binary digits and then those binary digits will be converted into hexadecimal. It can be any string that will be converted into hexadecimal format. A single string parameter is passed to the function which will get converted into a hexadecimal value or format. This function returns a hexadecimal value for the string which is passed as an argument to the function.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
bin2hex($String)
PHP bin2hex() is a function which considers one string value which can be any string type and that will get converted into hexadecimal value. The function returns the value which is fed as an input of the string to the function as a parameter.
bin2hex() function is a part of PHP which is used for converting the string into hexadecimal values. It can also consider binary format of string data, but it should not be made a misconception that only binary format of string will be converted into hexadecimal string as its name suggests for bin2hex. To be more specific the input string of ASCII characters gets converted into hexadecimal values. The string can be converted back into the previous format of string using pack function. It means that bin2hex string and pack function are good companions and are somewhat dependent on each other. This function being part of the PHP string references can take care of any string format data to be converted into the final string as an output for reference in hexadecimal format.
Also, the conversion of the string as a parameter to the bin2hex() function does not happen to take place in one go, conversion happens with the high nibble first performed byte wise. As mentioned, that the pack function is inevitable with the bin2hex() function which can be proved with a mere fact being observer at the time of execution. Suppose the string passed as a parameter is a binary format string which means an input in binary format is passed which will get converted into hexadecimal format as per requirement. But now the requirement has arrived where the binary format needs to be converted into same earlier format of the string in which it was fed as an input it clearly signifies a vast change in requirement then at that time pack() function comes as a savior. The pack() function as part of the PHP string references will consider the binary format of string, it will unpack the entire string based on some signed and unsigned format codes. Its functionality and the theme of coding and decoding or packing and unpacking all is almost similar as coding and decoding standard paradigm of Perl language. But still the functionality in bin2hex() functionality accompanied with pack function is not as flexible as the coding and decoding format of string in Perl language as themed.
Following are the example are given below:
This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is Welcome everyone to educba and output is the hexadecimal format of the passed string as shown in the output.
Code:
<?php<br />
$str = bin2hex("Welcome evreryone to educba!");<br />
echo($str);<br />
?>
Output:
This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is Welcome Educba! and output is the hexadecimal format of the passed string with its input string value as shown in the output.
Code:
<?php<br />
$str = "welcome Educba!";<br />
echo bin2hex($str) . "n";<br />
echo pack("H*",bin2hex($str)) . "n";<br />
?>
Output:
This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is string(17)and output is the dump of entire hexadecimal format of the string passed which is string(17) as shown in the output.
Code:
<?php<br />
$hex = hex2bin("657864545706c652657865782064617461");<br />
var_dump($hex);<br />
?>
Output:
This program makes use of the pack function as it is a good companion of the bin2hex () function string. It works same as bin2hex() function with a minute difference that it will consider only the binary format string with the no of values and number of parameters with 4 which is 90, 72, 80, 62 for the C4 string as an input string as output.
Code:
<?php<br />
echo pack("C4",90,72,80,62);<br />
?>
Output:
This program makes use of the pack function as it is a good companion of the bin2hex () function string. It works same as bin2hex() function with a minute difference that it will consider only the binary format string with the no of values and number of parameters with * which is 55, 65, 83 for the C* string as input string to the output. Here the C* represents that any number of length string can be passed as a parameter with any number of values.
Code:
<?php<br />
echo pack("C*",55,65,83);<br />
?>
Output:
This program illustrates the usage of unpack function of the hexadecimal string passed as a parameter and then it will unpack the string according to the format and then will generate back the binary data format of the input string.
Code:
<?php<br />
$data = "educba";<br />
print_r(unpack("C*",$data));<br />
?>
Output:
This program illustrates the usage of unpack function of the hexadecimal string passed as a parameter and then it will unpack any number of values(length) string according to the format and then will generate back the binary data format of the input string as shown in the output.
Code:
<?php<br />
$data = "educba";<br />
print_r(unpack("C*edu",$data));<br />
?>
Output:
PHP bin2hex() function works well with all the string which are part of string references and with the help of each high nibble pattern conversion makes it a unique function. Also, pack() and unpack() function makes it more versatile.
The above is the detailed content of PHP bin2hex(). For more information, please follow other related articles on the PHP Chinese website!