The bin2hex() and hex2bin() functions in PHP are used for converting between binary and hexadecimal formats. These functions are especially useful for data manipulation, encoding, and cryptographic purposes.
bin2hex() converts binary data into a readable hexadecimal stringhex2bin() converts hexadecimal data back into binary formatbin2hex(string $binary): stringhex2bin(string $hex): string|false
// Convert a plain string into hexadecimal format
<?php
$string = "hello";
$hex = bin2hex($string);
echo $hex;
?>
68656c6c6f
// Decode hexadecimal data back into its original string
<?php
$hex = "68656c6c6f";
$binary = hex2bin($hex);
echo $binary;
?>
hello
Binary → Hex → Binary
This conversion process is reversible and ensures safe transport of binary data through text-based systems.
hex2bin()bin2hex() and restore them