EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP bin2hex()

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP bin2hex()

PHP bin2hex()

Definition of PHP bin2hex()

PHP bin2hex() is a function which is used to convert any string to hexadecimal values. The binary string conversion is traversed through byte-wise with continuous conversions happening at the background. One very important aspect of this function is that bin2hex() does not represents that the string will represent binary digits and then those binary digits will be converted into hexadecimal. It can be any string that will be converted into hexadecimal format. A single string parameter is passed to the function which will get converted into a hexadecimal value or format. This function returns a hexadecimal value for the string which is passed as an argument to the function.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

bin2hex($String)

PHP bin2hex() is a function which considers one string value which can be any string type and that will get converted into hexadecimal value. The function returns the value which is fed as an input of the string to the function as a parameter.

How does bin2hex() Function Work in PHP?

bin2hex() function is a part of PHP which is used for converting the string into hexadecimal values. It can also consider binary format of string data, but it should not be made a misconception that only binary format of string will be converted into hexadecimal string as its name suggests for bin2hex. To be more specific the input string of ASCII characters gets converted into hexadecimal values. The string can be converted back into the previous format of string using pack function. It means that bin2hex string and pack function are good companions and are somewhat dependent on each other. This function being part of the PHP string references can take care of any string format data to be converted into the final string as an output for reference in hexadecimal format.

Also, the conversion of the string as a parameter to the bin2hex() function does not happen to take place in one go, conversion happens with the high nibble first performed byte wise. As mentioned, that the pack function is inevitable with the bin2hex() function which can be proved with a mere fact being observer at the time of execution. Suppose the string passed as a parameter is a binary format string which means an input in binary format is passed which will get converted into hexadecimal format as per requirement. But now the requirement has arrived where the binary format needs to be converted into same earlier format of the string in which it was fed as an input it clearly signifies a vast change in requirement then at that time pack() function comes as a savior. The pack() function as part of the PHP string references will consider the binary format of string, it will unpack the entire string based on some signed and unsigned format codes. Its functionality and the theme of coding and decoding or packing and unpacking all is almost similar as coding and decoding standard paradigm of Perl language. But still the functionality in bin2hex() functionality accompanied with pack function is not as flexible as the coding and decoding format of string in Perl language as themed.

Examples of PHP bin2hex()

Following are the example are given below:

Example #1

This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is Welcome everyone to educba and output is the hexadecimal format of the passed string as shown in the output.

Code:

<?php
$str = bin2hex("Welcome evreryone to educba!");
echo($str);
?>

Output:

PHP bin2hex()-1.1

Example #2

This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is Welcome Educba! and output is the hexadecimal format of the passed string with its input string value as shown in the output.

Code:

<?php
$str = "welcome Educba!";
echo bin2hex($str) . "\n";
echo pack("H*",bin2hex($str)) . "\n";
?>

Output:

PHP bin2hex()-1.2

Example #3

This program is used to convert the string given as a parameter to the function into hexadecimal value. In this the input string is string(17)and output is the dump of entire hexadecimal format of the string passed which is string(17) as shown in the output.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,694 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Code:

<?php
$hex = hex2bin("657864545706c652657865782064617461");
var_dump($hex);
?>

Output:

PHP bin2hex()-1.3

Example #4

This program makes use of the pack function as it is a good companion of the bin2hex () function string. It works same as bin2hex() function with a minute difference that it will consider only the binary format string with the no of values and number of parameters with 4 which is 90, 72, 80, 62 for the C4 string as an input string as output.

Code:

<?php
echo pack("C4",90,72,80,62);
?>

Output:

PHP bin2hex()-1.4

Example #5

This program makes use of the pack function as it is a good companion of the bin2hex () function string. It works same as bin2hex() function with a minute difference that it will consider only the binary format string with the no of values and number of parameters with * which is 55, 65, 83 for the C* string as input string to the output. Here the C* represents that any number of length string can be passed as a parameter with any number of values.

Code:

<?php
echo pack("C*",55,65,83);
?>

Output:

PHP bin2hex()-1.5

Example #6

This program illustrates the usage of unpack function of the hexadecimal string passed as a parameter and then it will unpack the string according to the format and then will generate back the binary data format of the input string.

Code:

<?php
$data = "educba";
print_r(unpack("C*",$data));
?>

 Output:

PHP bin2hex()-1.6

Example #7

This program illustrates the usage of unpack function of the hexadecimal string passed as a parameter and then it will unpack any number of values(length) string according to the format and then will generate back the binary data format of the input string as shown in the output.

Code:

<?php
$data = "educba";
print_r(unpack("C*edu",$data));
?>

Output:

PHP bin2hex()-1.7

Conclusion

PHP bin2hex() function works well with all the string which are part of string references and with the help of each high nibble pattern conversion makes it a unique function. Also, pack() and unpack() function makes it more versatile.

Recommended Articles

This is a guide to PHP bin2hex(). Here we also discuss how does bin2hex() function work in php? along with different examples and code its implementation. You may also have a look at the following articles to learn more –

  1. preg_match in PHP
  2. Reverse String in PHP
  3. PHP chop()
  4. PHP sha1()

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More