Home >Backend Development >PHP Problem >How to convert hexadecimal to corresponding characters in php
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:
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;
Output result:
abc
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.
Format | Description |
---|---|
NUL padding characters String | |
Space-filled string | |
Hexadecimal string (low digit First) | |
Hexadecimal string (highest order first) | |
Signed characters | |
Fill 1 byte blank | |
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;
Output result:
abc
4. Notes
hex2bin() function and pack() function are only supported by PHP5.4 and above.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!