Home > Article > Backend Development > PHP function bin2hex() that converts a string of ASCII characters into a hexadecimal value
Convert "Hello World!" to a hexadecimal value:
<?php $str = bin2hex("Hello World!"); echo($str); ?>
Definition and usage
bin2hex() function converts a string of ASCII characters into a hexadecimal value . Strings can be converted back using the pack() function.
Syntax
bin2hex(string)
Parameters Description
string Required. Specifies the string to be converted.
Technical details Return value:
Returns the hexadecimal value of the string to be converted.
Convert a string value from binary to hexadecimal, and then convert it back:
<?php $str = "Hello world!"; echo bin2hex($str) . "<br>"; echo pack("H*",bin2hex($str)) . "<br>"; ?>
//php中有 bin2hex方法,但没有 hex2bin方法,以下简单实现 hex2bin : <?php function hex2bin($data) { $len = strlen($data); return pack("H" . $len, $data); } ?>
The function of PHP bin2hex() is to convert a string of ASCII characters to hexadecimal system value.
bin2hex definition and usage
addAttribute() function adds an attribute to the SimpleXML element.
This function has no return value.
Syntax
class SimpleXMLElement { string addAttribute(name,value,ns) }
Parameters Description
name Required. Specifies the name of the attribute.
value Required. Specifies the value of the attribute.
ns Optional. Specifies the namespace of the attribute.
bin2hex example
XML file:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
PHP code:
<?php $xml = simplexml_load_file("test.xml"); $xml->body[0]->addAttribute("type", "small"); foreach($xml->body[0]->attributes() as $a => $b) { echo $a,'="',$b,'"'; } ?>
Output:
type="small"
The above is the detailed content of PHP function bin2hex() that converts a string of ASCII characters into a hexadecimal value. For more information, please follow other related articles on the PHP Chinese website!