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

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
CREATE DATABASE in PostgreSQLPostgresAvoid using new Array() in JavaScriptJavaScriptShould we ever delete data from a database?Miscellaneous2 Ways we can create an Array in JavaScriptJavaScriptfirst-of-type and last-of-type selectors in CSSCSSHow to catch .keypress() on body using jQuery?jQueryWhat are Identifiers in JavaScript?JavaScriptArguments in @mixin rules in SCSS (Sass)SCSSHow to hide and show menu on scroll using JavaScript and CSS?CSSHow to make a UILabel clickable in Swift 5?SwiftHow to include CSS and JavaScript files in your WordPress plugin?WordPressHow does @extend rule work in SCSS (Sass)?SCSS