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

How to convert array to hexadecimal in php

青灯夜游
青灯夜游Original
2022-04-29 17:08:522056browse

Conversion method: 1. Use the implode() function to convert the array into a string. The connector between elements is a null character. The syntax is "implode($arr)" or "implode("",$arr) "; 2. Use the bin2hex() function to convert the string into a hexadecimal value, the syntax is "bin2hex (string)".

How to convert array to hexadecimal in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, the array cannot be directly converted to Hexadecimal, but can be converted indirectly with the help of strings.

  • First use the implode() function to convert the array into a string

  • Then use the bin2hex() function to convert the string into hexadecimal System value

Implementation example:

<?php 
header("content-type:text/html;charset=utf-8");
$array = array(1,2,3,"a","Hello","World"); 
var_dump($array);       
$bin = implode("",$array);
echo "字符串:".$bin."<br>";
$hex = bin2hex($bin);
echo "十六进制值:".$hex."<br>";
?>

How to convert array to hexadecimal in php

Description:

implode() function returns an array element The combined string. (The alias of the implode() function is also called the join() function.)

implode(separator,array)
Parameter Description
separator Optional. Specifies what is placed between array elements. Default is "" (empty string).
array Required. Arrays to be combined into strings.

#bin2hex() function converts a string of ASCII characters into a hexadecimal value.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert array 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