Home >Backend Development >PHP Problem >How to convert binary to hexadecimal in php

How to convert binary to hexadecimal in php

藏色散人
藏色散人Original
2022-01-18 10:56:452597browse

php method to convert binary to hexadecimal: 1. Create a PHP sample file; 2. Use the bin2hex() function to convert a string of ASCII characters into a hexadecimal value.

How to convert binary to hexadecimal in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to convert binary to hexadecimal in php System?

Binary to hexadecimal

Function: bin2hex(string $str);

@param $str will be converted A string of ASCII characters.

              @return   Returns the hexadecimal value of the converted string.

Function description:

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

Bin2hex () Function to convert the byte method.

Example:

(1)bin2hex() converts 'chengdu' to hexadecimal value:

<?php
    $str = bin2hex(&#39;chengdu&#39;);
 
   echo $str;

The above program statement will output: 6368656e676475

(2) Convert a string value from binary to hexadecimal, and then convert it back:

<?php
    $str = &#39;chengdu&#39;;
         
    echo bin2hex($str) . "<br/>";
    echo pack("H*",  bin2hex($str)) . "<br/>";

The above program statements are output in sequence: 6368656e676475, chengdu

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert binary to hexadecimal in php. 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