Home  >  Article  >  Backend Development  >  PHP function bin2hex() that converts a string of ASCII characters into a hexadecimal value

PHP function bin2hex() that converts a string of ASCII characters into a hexadecimal value

PHP中文网
PHP中文网Original
2017-11-01 10:28:063003browse

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&#39;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,&#39;="&#39;,$b,&#39;"&#39;;
  }
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn