w3resource

PHP String Exercises : Get a hex dump of a string


16. Get Hex Dump of a String

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

For more Practice: Solve these Related Problems:

  • Write a PHP script to generate a hexadecimal representation of a string using unpack() or bin2hex().
  • Write a PHP function that takes a string and outputs its hex dump in a formatted multi-line block.
  • Write a PHP program to compare the hex dump of two strings and output any differences in their byte values.
  • Write a PHP script to create a hex dump function that displays both hexadecimal and ASCII representation of a given string.

Go to:


PREV : Remove Part of String from Beginning.
NEXT : Insert a String at a Specified Position.

PHP Code Editor:



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

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.