Home > Backend Development > PHP Tutorial > 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中文网
Release: 2023-03-16 21:18:02
Original
3052 people have browsed it

Convert "Hello World!" to a hexadecimal value:

<?php 
$str = bin2hex("Hello World!");
echo($str); 
?>
Copy after login

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)
Copy after login

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>";
?>
Copy after login
//php中有 bin2hex方法,但没有 hex2bin方法,以下简单实现 hex2bin :
<?php
function hex2bin($data) {
     $len = strlen($data);
     return pack("H" . $len, $data); } 
?>
Copy after login

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)
}
Copy after login

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>
Copy after login

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;;
  }
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template