Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

bin2hex

Converts the bytes of a string to hexadecimal.

Syntax

bin2hex(string $string): string

Parameters

ParameterTypeDescription
$stringstringA 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:

504850

A random token:

<?php
 
echo strlen(bin2hex(random_bytes(16)));

Output:

32

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.