PHP 8 reference · String Functions
PHP bin2hex() Function
Convert binary data to a hexadecimal string, two hex digits per byte. Often used to print random bytes as a token.
PHP 8 Since 4.0
Syntax
bin2hex(string $string): string
Parameters
| Parameter | Description |
|---|---|
string |
The input string. |
Return value
The hex string, twice as long as the input.
Examples
<?php
echo bin2hex("abc"), "\n";
echo hex2bin("616263"), "\n";
Output
616263 abc
<?php
$token = bin2hex(random_bytes(16));
echo strlen($token), "\n";
Output
32
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
Related
base64_encode()· Encode binary data or text as Base64, using only letters, digits, + and /, so it travels safely as text.ord()· Return the value (0 to 255) of the first byte of a string, such as 65 for "A". The reverse of chr().- PHP Strings · tutorial