bin2hex
Converts the bytes of a string to hexadecimal.
Syntax
bin2hex(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | A string of bytes. |
Return value
stringA string of hexadecimal digits — twice as long as the input.
Examples
The bytes of a string:
<?php
echo bin2hex('PHP');Output:
504850A random token:
<?php
echo strlen(bin2hex(random_bytes(16)));Output:
32Notes
hex2bin() does the reverse conversion.
See also
- random_bytes — Returns cryptographically secure random bytes.
- dechex — Converts a number to hexadecimal.
- base64_encode — Encodes data as text made of ASCII letters and digits.
Related recipes
In the tutorial
- Security — The basics of PHP site security — XSS and escaping, SQL injection, password hashing, CSRF tokens and validating input.