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

转载于:https://my.oschina.net/thmz/blog/77057

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值