How does the bin2hex() function work in PHP?

PHPServer Side ProgrammingProgramming

It is a function that helps in converting a string into a hexadecimal value. It is done byte wise and the high nibbled bytes are converted first.

Syntax of bin2hex() function

bin2hex($string)

It takes in a string and converts the string to a hexadecimal representation.

Example

 Live Demo

<?php
   $my_str1 = "This";
   $my_str2 = "is";
   $my_str3 = "a";
   $my_str4 = "sample";
   print(bin2hex($my_str1));
   echo "<br>";
      print(bin2hex($my_str2));
   echo "<br>";
      print(bin2hex($my_str3));
   echo "<br>";
      print(bin2hex($my_str4));
?>

Output

54686973
6973
61
73616d706c65

Within the <php> tags, the bin2hex function is called on 4 elements which are previously defined. Then, the result of calling the bin2hex function on each of the string is displayed on the console.

raja
Published on 01-Jul-2020 10:38:02
Advertisements