请教一个函数 pack() && unpack
请教一个函数 pack() && unpack
$string="abcd";
$temp = pack(C1,$string);
我怎么得到的32呢?pack做了什么工作?
pack
Syntax
Category list operator (records)
Arguments template, list
Return Value string
Definition
This function returns a packed version of the data in the list using the template to determine how it is coded. The template comprises a sequence of characters, each specifying the data type of the matching data item in the list.
Character
Description
@
Null fill to absolute position
A
ASCII string with spaces to pad
a
ASCII string with nulls to pad
b
Bit string (ascending bit order)
B
Bit string (descending bit order)
c
Signed char value
C
Unsigned char value
d
Double-precision float in the native format
f
Single-precision float in the native format
h
Hex string (low nybble first)
H
Hex string (high nybble first)
i
Signed integer value
I
Unsigned integer value
l
Signed long integer value
L
Unsigned long integer value
n
Short integer "network" order
N
Long integer "network" order
p
Pointer to a null-terminated string
P
Pointer to a structure (fixed-length string)
s
Signed short integer value
S
Unsigned short integer value
u
UUencoded string
v
Short integer "VAX" (little-endian) order
V
Long integer "VAX" (little-endian) order
x
Null byte
X
Back up a byte
A concise form of template can be used by appending a number after any letter to repeat that format specifier. for aA, the number uses one value and pads the rest. for bB, the number indicates the number of bits. for hH, the number indicates the number of nybbles. for P, the number indicates the size of the pointer structure. Using an asterisk in place of a number means to repeat the format specifier as necessary to use up all list values. Note that some packed structures may not be portable across machines (in particular, network and floating point formats). It should be possible to unpack the data using the same format specification with an unpack() call.
Example
Use Socketl
@a=(140,203,7,103);
$addr=pack(C4,@a);
($name,$alias,$adrtype,$length,@address)=getostbyaddrh
($addr,Af_INET);print("pack() ",@a, "packed as: $addr". ");