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

How to convert hexadecimal to corresponding characters in php

PHPz
Release: 2023-04-12 19:09:39
Original
1485 people have browsed it

PHP provides a function for converting hexadecimal characters, which can easily convert hexadecimal strings into corresponding characters. This article will introduce the usage and precautions of this function.

1. Function introduction

PHP provides two functions that can be used to convert hexadecimal strings into characters:

  1. hex2bin() function : Convert hexadecimal string to binary string.
  2. pack() function: Pack one or more integers, floating point numbers or strings into a binary string.

2. Usage of hex2bin() function

Syntax: hex2bin($hexstr)

Function: Convert a hexadecimal string to the corresponding Binary string.

Sample code:

$hexstr = '616263';
$binstr = hex2bin($hexstr);
echo $binstr;
Copy after login

Output result:

abc
Copy after login
Copy after login

3. Usage of pack() function

Syntax: pack($format, $data1, $data2, ...)

Role: Pack one or more integers, floating point numbers or strings into a binary string.

$format parameter: Specify the packaging format, see the table below for details.

##aNUL padding characters StringASpace-filled stringhHexadecimal string (low digit First)HHexadecimal string (highest order first)cSigned charactersxFill 1 byte blank##CsSnviIlLNVfdSample code:
Format Description
Unsigned characters
Signed short (16 bits, endian depends on host)
Unsigned short integer (16 bits, big and small endian depends on the host)
Unsigned short integer (16 bits, big endian)
Unsigned short (16 bits, little endian)
Signed integer type (32 bits, big and small endian depends on the host)
Unsigned integer type (32 bits, big and small endian depends on the host)
Signed long integer (32 bits, endian depends on the host)
Unsigned long integer (32 bits, big and small endian depends on the host)
Unsigned long integer (32 bits, big endian) order)
Unsigned long integer (32 bits, little endian)
Single precision floating point number
Double precision floating point number
$hexstr = '616263';
$binstr = pack('H*', $hexstr);
echo $binstr;
Copy after login

Output result:

abc
Copy after login
Copy after login

4. Notes

hex2bin() function and pack() function are only supported by PHP5.4 and above.
  1. The parameters of the hex2bin() function and pack() function must be hexadecimal strings with an even number of digits, otherwise an error will be reported.
  2. The first parameter of the pack() function must specify the packaging format, and generally the endianness needs to be selected based on the target encoding.

The above is the detailed content of How to convert hexadecimal to corresponding characters in php. For more information, please follow other related articles on the PHP Chinese website!

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