如何使用PHP将Hex转换为Byte

时间:2015-06-03 07:36:03

标签: php hex byte

我目前在使用PHP将Hex值转换为byte时遇到问题。有没有办法做到这一点?

这是我要转换为字节的十六进制值:02 05 12 E6 A3

我已经尝试了pack()函数和chr()函数,但它没有帮助我。我能否知道有什么方法可以解决这个问题。提前致谢

1 个答案:

答案 0 :(得分:1)

为此任务选择Pack()函数。 pack()函数将数据打包成二进制字符串。它的语法是pack(format,args+)format是必填参数,而args是可选的。它指定打包数据时使用的格式。有如下各种格式:

a - NUL-padded string
A - SPACE-padded string
h - Hex string, low nibble first
H - Hex string, high nibble first
c - signed char
C - unsigned char
s - signed short (always 16 bit, machine byte order)
S - unsigned short (always 16 bit, machine byte order)
n - unsigned short (always 16 bit, big endian byte order)
v - unsigned short (always 16 bit, little endian byte order)
i - signed integer (machine dependent size and byte order)
I - unsigned integer (machine dependent size and byte order)
l - signed long (always 32 bit, machine byte order)
L - unsigned long (always 32 bit, machine byte order)
N - unsigned long (always 32 bit, big endian byte order)
V - unsigned long (always 32 bit, little endian byte order)
f - float (machine dependent size and representation)
d - double (machine dependent size and representation)
x - NUL byte
X - Back up one byte
Z - NUL-padded string
@ - NUL-fill to absolute position

注意:在PHP 5.5中添加了“Z”代码,其功能与“a”相同,用于Perl兼容性