PHP converts a string of ASCII characters to a hexadecimal value function Bin2Hex ()

Source: Internet
Author: User
Convert "Hello world!" to a hexadecimal value:

<?php $str = Bin2Hex ("Hello world!"); Echo ($STR);?>

Definition and usage

The Bin2Hex () function converts a string of ASCII characters to a hexadecimal value. Strings can be converted back by using the pack () function.

Grammar

Bin2Hex (String)

Parameter description

string is required. Specifies the string to convert.

Technical details return value:

Returns the hexadecimal value of the string to be converted.

Convert a string value from binary to 16 and back again:

<?php$str = "Hello world!"; echo Bin2Hex ($STR). "<br>"; Echo Pack ("h*", Bin2Hex ($STR)). "<br>";? >
PHP has the Bin2Hex method, but there is no Hex2bin method, the following simple implementation hex2bin: <?phpfunction Hex2bin ($data) {     $len = strlen ($data);     Return Pack ("H". $len, $data); }?>

The function of PHP Bin2Hex () is to convert a string of ASCII characters to a hexadecimal value.

Bin2Hex Definition and usage

The AddAttribute () function adds an attribute to the SimpleXML element.

The function has no return value.

Grammar

Class simplexmlelement{string AddAttribute (Name,value,ns)}

Parameter description

Name is required. The name of the specified property.

Value is required. The value of the specified property.

NS is optional. Specifies the namespace of the attribute.

Bin2Hex instances

XML file:

<?xml version= "1.0" encoding= "iso-8859-1"? ><note><to>george</to><from>john</ From>

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.