Yogesh Chauhan's Blog

bin2hex() and chr() String Functions in PHP

in PHP on July 6, 2020

PHP String Functions Series Part 2

bin2hex() Function [PHP Version: 4+]

Converts binary data into hexadecimal representation.

Returns an ASCII string containing the hexadecimal representation of str. The conversion is done byte-wise with the high-nibble first.

Syntax


bin2hex ($str)

where str = the string to be converted, it’s required

Example


$str = bin2hex("YogeshChauhan.com");
echo($str); 

//output

596f676573684368617568616e2e636f6d

chr() Function [PHP Version: 4+]

Generate a single-byte string from a number

Syntax


chr(enter_ascii_value)

Returns a one-character string containing the character specified by interpreting bytevalue as an unsigned integer.

bytevalue: An integer between 0 and 255.

Values outside the valid range (0..255) will be bitwise and’ed with 255, which is equivalent to the following algorithm:


while ($bytevalue < 0) {
    $bytevalue += 256;
}
$bytevalue %= 256;

Example


$str = chr(046);
echo $str; // &

Sources


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Understand Inheritance in PHP in this Basic Example (For Beginners)PHPAmpersand (Parent Selector) in SCSS (Sass)SCSSWordPress: How to display slider repeater fields in ACF?WordPressHow JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptSolution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’SwiftMicroservices vs Monolithic ArchitectureMiscellaneousComposing and Extracting Components in ReactReactHow to Validate User Name, Email Address and URL in PHP?PHPHow to load a module with configuration in SCSS?SCSSNew to  Drupal? Here’s What You Need to KnowDrupalShort-Circuit Evaluation in JavaScriptJavaScriptColleague UI Basics: The Search AreaColleague