Home > Backend Development > PHP Tutorial > How Can I Get a Hex Dump of a String in PHP?

How Can I Get a Hex Dump of a String in PHP?

Patricia Arquette
Release: 2024-12-19 09:39:08
Original
762 people have browsed it

How Can I Get a Hex Dump of a String in PHP?

Getting a Hex Dump of a String in PHP

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);
Copy after login

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);
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template