Ruby String#unpack
来源:互联网 发布:fgo羁绊礼装 知乎 编辑:程序博客网 时间:2024/06/03 18:16
从一个例子开始:如何将一个字符串转化成一个十六进制的串。一种方法是使用字符串的each_byte来做:
- res = []
- "abc".each_byte {|i| res << i}
- res.map {|i| i.to_s(16)}
写了三行才实现出来这个功能,非常不爽。我们看看unpack的强大功能吧:
- "abc".unpack('H*')
一行代码搞定。
unpack将传入的参数作为指令,作用于调于的字符串,处理过程是一个流式的过程,每一个字符指令后面可能跟着一个数字,数字代表顺序作用于字符串字符的次数,*代表顺序作用域剩余的所有字符。
上面例子H*指令,代表每次取半字节的16进制数。
unpack可以非常方便的处理字符串的各种转换。
以下字符指令的参考表(来自rdoc):
Format | Returns | Function
-------+---------+-----------------------------------------
A | String | with trailing nulls and spaces removed
-------+---------+-----------------------------------------
a | String | string
-------+---------+-----------------------------------------
B | String | extract bits from each character (msb first)
-------+---------+-----------------------------------------
b | String | extract bits from each character (lsb first)
-------+---------+-----------------------------------------
C | Fixnum | extract a character as an unsigned integer
-------+---------+-----------------------------------------
c | Fixnum | extract a character as an integer
-------+---------+-----------------------------------------
d,D | Float | treat sizeof(double) characters as
| | a native double
-------+---------+-----------------------------------------
E | Float | treat sizeof(double) characters as
| | a double in little-endian byte order
-------+---------+-----------------------------------------
e | Float | treat sizeof(float) characters as
| | a float in little-endian byte order
-------+---------+-----------------------------------------
f,F | Float | treat sizeof(float) characters as
| | a native float
-------+---------+-----------------------------------------
G | Float | treat sizeof(double) characters as
| | a double in network byte order
-------+---------+-----------------------------------------
g | Float | treat sizeof(float) characters as a
| | float in network byte order
-------+---------+-----------------------------------------
H | String | extract hex nibbles from each character
| | (most significant first)
-------+---------+-----------------------------------------
h | String | extract hex nibbles from each character
| | (least significant first)
-------+---------+-----------------------------------------
I | Integer | treat sizeof(int) (modified by _)
| | successive characters as an unsigned
| | native integer
-------+---------+-----------------------------------------
i | Integer | treat sizeof(int) (modified by _)
| | successive characters as a signed
| | native integer
-------+---------+-----------------------------------------
L | Integer | treat four (modified by _) successive
| | characters as an unsigned native
| | long integer
-------+---------+-----------------------------------------
l | Integer | treat four (modified by _) successive
| | characters as a signed native
| | long integer
-------+---------+-----------------------------------------
M | String | quoted-printable
-------+---------+-----------------------------------------
m | String | base64-encoded
-------+---------+-----------------------------------------
N | Integer | treat four characters as an unsigned
| | long in network byte order
-------+---------+-----------------------------------------
n | Fixnum | treat two characters as an unsigned
| | short in network byte order
-------+---------+-----------------------------------------
P | String | treat sizeof(char *) characters as a
| | pointer, and return \emph{len} characters
| | from the referenced location
-------+---------+-----------------------------------------
p | String | treat sizeof(char *) characters as a
| | pointer to a null-terminated string
-------+---------+-----------------------------------------
Q | Integer | treat 8 characters as an unsigned
| | quad word (64 bits)
-------+---------+-----------------------------------------
q | Integer | treat 8 characters as a signed
| | quad word (64 bits)
-------+---------+-----------------------------------------
S | Fixnum | treat two (different if _ used)
| | successive characters as an unsigned
| | short in native byte order
-------+---------+-----------------------------------------
s | Fixnum | Treat two (different if _ used)
| | successive characters as a signed short
| | in native byte order
-------+---------+-----------------------------------------
U | Integer | UTF-8 characters as unsigned integers
-------+---------+-----------------------------------------
u | String | UU-encoded
-------+---------+-----------------------------------------
V | Fixnum | treat four characters as an unsigned
| | long in little-endian byte order
-------+---------+-----------------------------------------
v | Fixnum | treat two characters as an unsigned
| | short in little-endian byte order
-------+---------+-----------------------------------------
w | Integer | BER-compressed integer (see Array.pack)
-------+---------+-----------------------------------------
X | --- | skip backward one character
-------+---------+-----------------------------------------
x | --- | skip forward one character
-------+---------+-----------------------------------------
Z | String | with trailing nulls removed
| | upto first null with *
-------+---------+-----------------------------------------
@ | --- | skip to the offset given by the
| | length argument
-------+---------+-----------------------------------------
- Ruby String#unpack
- Ruby String#unpack
- ruby Array#pack String#unpack 实例
- ruby/python利用pack unpack读取二进制文件
- ruby String
- UNPACK
- UNPACK
- unpack
- struct.error: unpack requires a string argument of length 4
- pack和unpack格式化字符串(format string)解释
- Ruby String类(三)
- ruby String substitution
- Ruby的数据类型String
- Ruby String常用函数
- Ruby symbol String
- json and string (Ruby)
- Ruby:String转Time
- Ruby String(2)
- win7下access驱动安装
- Erlang的lists操作(详细)
- 关于load average
- 内核模式 注册表编程
- 关于struts标签bean:message
- Ruby String#unpack
- Android菜鸟如何学习Android系统开发?(
- HTML5 里面 input元素的属性
- Java I/O流-ObjectInputStream、ObjectOutputStream
- rpmbuild制作rpm包
- 【每周一译】如何贡献开源代码
- Android声音资源的使用
- ios项目应用图标icon和default图片命名规则
- 转:先序中序后序遍历二叉树