pack(p1) public

Packs the contents of arr into a binary sequence according to the directives in aTemplateString (see the table below) Directives “A,” “a,” and “Z” may be followed by a count, which gives the width of the resulting field. The remaining directives also may take a count, indicating the number of array elements to convert. If the count is an asterisk (“*”), all remaining array elements will be converted. Any of the directives “sSiIlL” may be followed by an underscore (“_”) to use the underlying platform’s native size for the specified type; otherwise, they use a platform-independent size. Spaces are ignored in the template string. See also String#unpack.

a = [ "a", "b", "c" ]
n = [ 65, 66, 67 ]
a.pack("A3A3A3")   #=> "a  b  c  "
a.pack("a3a3a3")   #=> "a\000\000b\000\000c\000\000"
n.pack("ccc")      #=> "ABC"

Directives for pack.

Directive Meaning

@     |  Moves to absolute position
A     |  arbitrary binary string (space padded, count is width)
a     |  arbitrary binary string (null padded, count is width)
B     |  Bit string (descending bit order)
b     |  Bit string (ascending bit order)
C     |  Unsigned byte (C unsigned char)
c     |  Byte (C char)
D, d  |  Double-precision float, native format
E     |  Double-precision float, little-endian byte order
e     |  Single-precision float, little-endian byte order
F, f  |  Single-precision float, native format
G     |  Double-precision float, network (big-endian) byte order
g     |  Single-precision float, network (big-endian) byte order
H     |  Hex string (high nibble first)
h     |  Hex string (low nibble first)
I     |  Unsigned integer
i     |  Integer
L     |  Unsigned long
l     |  Long
M     |  Quoted printable, MIME encoding (see RFC2045)
m     |  Base64 encoded string (see RFC 2045, count is width)
      |  (if count is 0, no line feed are added, see RFC 4648)
N     |  Long, network (big-endian) byte order
n     |  Short, network (big-endian) byte-order
P     |  Pointer to a structure (fixed-length string)
p     |  Pointer to a null-terminated string
Q, q  |  64-bit number
S     |  Unsigned short
s     |  Short
U     |  UTF-8
u     |  UU-encoded string
V     |  Long, little-endian byte order
v     |  Short, little-endian byte order
w     |  BER-compressed integer\fnm
X     |  Back up a byte
x     |  Null byte
Z     |  Same as ``a'', except that null is added with *
Show source
Register or log in to add new notes.