Project1

标题: 美兽看!(pack的资料,我先前说的似乎有错) [打印本页]

作者: 轮回者    时间: 2006-11-20 20:57
标题: 美兽看!(pack的资料,我先前说的似乎有错)
本帖最后由 后知后觉 于 2009-12-13 16:28 编辑

unpack  str.unpack( format ) -> anArray  

Decodes str (which may contain binary data) according to the format string, returning an array of each value extracted. The format string consists of a sequence of single-character directives, summarized in Table 22.8 on page 379. Each directive may be followed by a number, indicating the number of times to repeat with this directive. An asterisk (``*'') will use up all remaining elements. The directives sSiIlL may each be followed by an underscore (``_'') to use the underlying platform's native size for the specified type; otherwise, it uses a platform-independent consistent size. Spaces are ignored in the format string. See also Array#pack on page 286.
Directives for String#unpack Format  Function  Returns  
A String with trailing nulls and spaces removed. String
a String. String
B Extract bits from each character (msb first). String
b Extract bits from each character (lsb first). String
C Extract a character as an unsigned integer. Fixnum
c Extract a character as an integer. Fixnum
d Treat sizeof(double) characters as a native double. Float
E Treat sizeof(double) characters as a double in little-endian byte order. Float
e Treat sizeof(float) characters as a float in little-endian byte order. Float
f Treat sizeof(float) characters as a native float. Float
G Treat sizeof(double) characters as a double in network byte order. Float
g Treat sizeof(float) characters as a float in network byte order. Float
H Extract hex nibbles from each character (most significant first). String
h Extract hex nibbles from each character (least significant first). String
I Treat sizeof(int) 1 successive characters as an unsigned native integer. Integer
i Treat sizeof(int) 1 successive characters as a signed native integer. Integer
L Treat four1 successive characters as an unsigned native long integer. Integer
l Treat four1 successive characters as a signed native long integer. Integer
M Extract a quoted-printable string. String
m Extract a base64 encoded string. String
N Treat four characters as an unsigned long in network byte order. Fixnum
n Treat two characters as an unsigned short in network byte order. Fixnum
P Treat sizeof(char *) characters as a pointer, and return len characters from the referenced location. String
p Treat sizeof(char *) characters as a pointer to a null-terminated string. String
S Treat two1 successive characters as an unsigned short in native byte order. Fixnum
s Treat two1 successive characters as a signed short in native byte order. Fixnum
U Extract UTF-8 characters as unsigned integers. Integer
u Extract a UU-encoded string. String
V Treat four characters as an unsigned long in little-endian byte order. Fixnum
v Treat two characters as an unsigned short in little-endian byte order. Fixnum
X Skip backward one character. ---
x Skip forward one character. ---
Z String with trailing nulls removed. String
@ Skip to the offset given by the length argument. ---



1 May be modified by appending ``_'' to the directive.



"abc \0\0abc \0\0".unpack('A6Z6')  » ["abc", "abc "]  
"abc \0\0".unpack('a3a3')  » ["abc", " \000\000"]  
"aa".unpack('b8B8')  » ["10000110", "01100001"]  
"aaa".unpack('h2H2c')  » ["16", "61", 97]  
"\xfe\xff\xfe\xff".unpack('sS')  » [-2, 65534]  
"now=20is".unpack('M*')  » ["now is"]  
"whole".unpack('xax2aX2aX1aX2a')  » ["h", "e", "l", "l", "o"]  

[LINE]1,#dddddd[/LINE]本贴由论坛斑竹灼眼的夏娜结贴,如楼主认为问题未解决,请重新将此贴编辑为“有事请教”,并回帖叙述疑点即可~ ^-^
作者: 轮回者    时间: 2006-11-20 20:57
标题: 美兽看!(pack的资料,我先前说的似乎有错)
本帖最后由 后知后觉 于 2009-12-13 16:27 编辑

重复内容……
作者: 美兽    时间: 2006-11-20 23:15
以下引用轮回者于2006-11-20 12:57:06的发言:

unpack  str.unpack( format ) -> anArray  


{/pz}...
http://rubycn.ce-lab.net/man/pack_template_string.html

只是好奇而已,不会真的用到的...谢谢了.





欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1