PHP bin2hex() function with example

By IncludeHelp Last updated : December 27, 2023

PHP bin2hex() function

The bin2hex() function is a string function, it is used to convert a given string of ASCII characters to the hexadecimal values.

Syntax

The syntax of the bin2hex() function:

bin2hex(string);

Parameters

The parameters of the bin2hex() function:

  • string: An input parameter.

Return Value

The return value of this method is string, it returns the hexadecimal converted string.

Sample Input/Output

Input:
str = Hello world
Output:
hex_str = 48656c6c6f20776f726c64

Input:
str = ABC 123 @#$%. XYZ
Output:
hex_str = 4142432031323320402324252e2058595a

Example of PHP bin2hex() Function

<?php
$str = "Hello world";
$hex_str = bin2hex($str);
    
//printing both strings
echo ("str = " . $str . "\n");
echo ("hex_str = " . $hex_str . "\n");
    
$str = "ABC 123 @#$%. XYZ";
$hex_str = bin2hex($str);
    
//printing both strings
echo ("str = " . $str . "\n");
echo ("hex_str = " . $hex_str . "\n");    
?>

Output

The output of the above example is:

str = Hello world
hex_str = 48656c6c6f20776f726c64
str = ABC 123 @#$%. XYZ
hex_str = 4142432031323320402324252e2058595a

To understand the above example, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.