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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
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 <br />
$str = bin2hex("Welcome evreryone to educba!");<br>
echo($str);<br>
?>
Output:
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 <br />
$str = "welcome Educba!";<br>
echo bin2hex($str) . "n";<br>
echo pack("H*",bin2hex($str)) . "n";<br>
?>
Output:
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.
Code:
<?php <br />
$hex = hex2bin("657864545706c652657865782064617461");<br>
var_dump($hex);<br>
?>
Output:
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 <br />
echo pack("C4",90,72,80,62);<br>
?>
Output:
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 <br />
echo pack("C*",55,65,83);<br>
?>
Output:
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 <br />
$data = "educba";<br>
print_r(unpack("C*",$data));<br>
?>
Output:
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 <br />
$data = "educba";<br>
print_r(unpack("C*edu",$data));<br>
?>
Output:
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.
The above is the detailed content of PHP bin2hex(). For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.