In PHP, obtaining a hex dump of a string, where each byte is represented in hexadecimal, is a useful technique for debugging encoding issues. Here are two methods to achieve this:
Method 1:
Using the bin2hex() function, you can convert a string into a hexadecimal representation:
echo bin2hex($string);
Method 2:
Iterating through the string character by character and converting each byte to hexadecimal using ord() and dechex():
for ($i = 0; $i < strlen($string); $i++) { echo str_pad(dechex(ord($string[$i])), 2, '0', STR_PAD_LEFT); }
In these examples, $string is the variable that holds the input string for which you want to create a hex dump.
The above is the detailed content of How Can I Get a Hex Dump of a String in PHP?. For more information, please follow other related articles on the PHP Chinese website!