PHP bin2hex() Function
The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value.
Syntax
1 2 3 |
bin2hex(str) |
Parameter
str(Required): This parameter represents the string to be converted into hexadecimal.
Return
This function returns the hexadecimal representation for the specified string.
Example 1
1 2 3 4 5 6 7 8 9 |
<?php // initializing the string $str ="Welcome to my World!"; // printing the string with ASCII charater echo "The ASCII character is: ".$str; echo "\n"."The hexadecimal value: ".bin2hex($str); ?> |
Output
1 2 3 4 |
The ASCII character is: Welcome to my World! The hexadecimal value: 57656c636f6d6520746f206d7920576f726c6421 |
Example 2
1 2 3 4 5 6 7 8 9 |
<?php // initializing the string with special characters $str ="#Welcome@ *to@ @my@ &World$!"; // printing the string with ASCII charater echo "The ASCII character is: ".$str; echo "\n"."The hexadecimal value: ".bin2hex($str); ?> |
Output
1 2 3 4 |
The ASCII character is: #Welcome@ *to@ @my@ &World$! The hexadecimal value: 2357656c636f6d6540202a746f402020406d7940202026576f726c642421 |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // initializing the string with binary values $str ="111011"; // printing the string echo "The Binary value: ".$str; # converting the binary to hexadecimal echo "\n"."The hexadecimal value: ".bin2hex($str); $hex = dechex(bindec($str)); echo "\nConverted Binary to Hexadecimal: ".$hex; ?> |
Output
1 2 3 4 5 |
The Binary value: 111011 The hexadecimal value: 313131303131 Converted Binary to Hexadecimal: 3b |
Example 4
1 2 3 4 5 6 7 8 9 10 11 |
<?php // initializing the string with NULL $str =NULL; // printing the string echo "The Binary value: ".$str; # converting the binary to hexadecimal # will return NULL echo "\n"."The hexadecimal value: ".bin2hex($str); ?> |
Output
1 2 3 4 |
The Binary value: The hexadecimal value: |