msgbartop
PHP语言, PHP扩展, Zend引擎相关的研究,技术,新闻分享 – 左手代码 右手诗
msgbarbottom

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

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

$cnStr = "中"; //utf8的中文

//unicode
$code = unpack("H6codes", $cnStr);

//汉字
$cnStr = pack("H6", $code['codes']);

恩, pack/unpack很强大,, 和c语言交换数据, 二进制方式的序列化,操作二进制文件.. etc…

附上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,实际使用的时候作为从开头跳到某字节用.


分享到:



Related Posts:

Tags: , ,

5 Responses to “汉字和Unicode码(utf-8)之间的转换(Pack/Unpack)”

  1. Unicode and UTF-8 | 火丁笔记 |

    [...] unpack [...]

  2. Unicode and UTF-8-SuperMan's blog |

    [...] unpack [...]

  3. hilojack |

    vim下,ga查看unicode,g8查看uft8编码,:%!xxd查看所有字符的存储编码(其实是把数据通过xxd转为16进制)

  4. 汉字和Unicode码(utf-8)之间的转换(Pack/Unpack) | 万维网黑客联盟 |

    [...] 本文地址: http://www.laruence.com/2008/05/01/106.html [...]

  5. NoAngels |

    Reflection可以用来写框架的系统核心

Leave a Reply

*