w3resource

PHP String Exercises : Get a hex dump of a string

PHP String: Exercise-16 with Solution

Write a PHP script to get a hex dump of a string.

Sample string : '[email protected]'

Sample Solution:

PHP Code:

<?php
$str = '[email protected]';
// Define the string.

echo bin2hex($str)."\n";
// Convert the string to hexadecimal representation and output it.
?>

Output:

72617979406578616d706c652e636f6d

Explanation:

The above PHP code snippet converts the string '[email protected]' into its hexadecimal representation using the "bin2hex()" function. The "bin2hex() function converts binary data into hexadecimal format. Here, it treats each character in the string as a byte and converts it to its corresponding hexadecimal representation. Finally, the hexadecimal representation is echoed out.

Flowchart :

Flowchart: Get a hex dump of a string

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to remove a part of a string from the beginning.
Next: Write a PHP script to insert a string at the specified position in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.