zoukankan      html  css  js  c++  java
  • 汉字和Unicode码(utf8)之间的转换(Pack/Unpack)

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

    1. $cnStr = "中"; //utf8的中文
    2.  
    3. //unicode
    4. $code = unpack("H6codes", $cnStr);
    5.  
    6. //汉字
    7. $cnStr = pack("H6", $code['codes']);

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

    附上format参数的说明:

    1. a      NUL-padded string, 即"\0"作为"空字符"的表示形式
    2. A      SPACE-padded string, 空格作为"空字符"的表示形式
    3. h      Hex string, low nibble first,升序位顺序
    4. H      Hex string, high nibble first,降序位顺序
    5. c      signed char, 有符号单字节
    6. C      unsigned char, 无符号单字节
    7. s      signed short (always 16 bit, machine byte order)
    8. S      unsigned short (always 16 bit, machine byte order)
    9. n      unsigned short (always 16 bit, big endian byte order)
    10. v      unsigned short (always 16 bit, little endian byte order)
    11. i      signed integer (machine dependent size and byte order)
    12. I      unsigned integer (machine dependent size and byte order)
    13. l      signed long (always 32 bit, machine byte order)
    14. L      unsigned long (always 32 bit, machine byte order)
    15. N      unsigned long (always 32 bit, big endian byte order)
    16. V      unsigned long (always 32 bit, little endian byte order)
    17. f      float (machine dependent size and representation)
    18. d      double (machine dependent size and representation)
    19. x      NUL byte, 实际使用的时候作为跳过多少字节用,很有用
    20. X      Back up one byte, 后退1字节
    21. @      NUL-fill to absolute position,实际使用的时候作为从开头跳到某字节用.
  • 相关阅读:
    ADB常用命令
    HttpClient4.x 使用cookie保持会话
    HttpClient发起Http/Https请求工具类
    MakeFile 文件的使用
    SQL连接查询和嵌套查询详解
    Linux中进程控制块PCB-------task_struct结构体结构
    Mysql数据库--语句整理/提升/进阶/高级使用技巧
    进程池分析
    mmap和shm共享内存的区别和联系
    IO多路复用模型之epoll实现机制
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1901769.html
Copyright © 2011-2022 走看看