汉字和Unicode码(utf-8)之间的转换(Pack/Unpack)

本文地址: http://www.laruence.com/2012/05/02/2613.html

保证你要转换的字符串编码为UTF8,如果不是,请iconv cnStr成utf8

<?php
$cnStr = "中";
$code = unpack("H6codes", $cnStr);
print_r($code);
$cnStr = pack("H6", $code['codes']);
print_r($cnStr);
?>

运行结果

Array
(
    [codes] => e4b8ad
)
中

format参数说明

a      NUL-padded string, 即"\0"作为"空字符"的表示形式
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, 后退1字节
@      NUL-fill to absolute position,实际使用的时候作为从开头跳到某字节用.

你可能感兴趣的:(汉字和Unicode码(utf-8)之间的转换(Pack/Unpack))