Skip to content

Instantly share code, notes, and snippets.

@jamesejr
Created August 25, 2013 18:56
Show Gist options
  • Save jamesejr/6335566 to your computer and use it in GitHub Desktop.
Save jamesejr/6335566 to your computer and use it in GitHub Desktop.
A list of Ruby directives for 'pack' e.g. [1,2,3].pack("CCn") => "\x01\x02\x00\x03"
Integer | Array |
Directive | Element | Meaning
---------------------------------------------------------------------------
C | Integer | 8-bit unsigned (unsigned char)
S | Integer | 16-bit unsigned, native endian (uint16_t)
L | Integer | 32-bit unsigned, native endian (uint32_t)
Q | Integer | 64-bit unsigned, native endian (uint64_t)
| |
c | Integer | 8-bit signed (signed char)
s | Integer | 16-bit signed, native endian (int16_t)
l | Integer | 32-bit signed, native endian (int32_t)
q | Integer | 64-bit signed, native endian (int64_t)
| |
S_, S! | Integer | unsigned short, native endian
I, I_, I! | Integer | unsigned int, native endian
L_, L! | Integer | unsigned long, native endian
| |
s_, s! | Integer | signed short, native endian
i, i_, i! | Integer | signed int, native endian
l_, l! | Integer | signed long, native endian
| |
S> L> Q> | Integer | same as the directives without ">" except
s> l> q> | | big endian
S!> I!> | | (available since Ruby 1.9.3)
L!> | | "S>" is same as "n"
s!> i!> | | "L>" is same as "N"
l!> | |
| |
S< L< Q< | Integer | same as the directives without "<" except
s< l< q< | | little endian
S!< I!< | | (available since Ruby 1.9.3)
L!< | | "S<" is same as "v"
s!< i!< | | "L<" is same as "V"
l!< | |
| |
n | Integer | 16-bit unsigned, network (big-endian) byte order
N | Integer | 32-bit unsigned, network (big-endian) byte order
v | Integer | 16-bit unsigned, VAX (little-endian) byte order
V | Integer | 32-bit unsigned, VAX (little-endian) byte order
| |
U | Integer | UTF-8 character
w | Integer | BER-compressed integer
Float | |
Directive | | Meaning
---------------------------------------------------------------------------
D, d | Float | double-precision, native format
F, f | Float | single-precision, native format
E | Float | double-precision, little-endian byte order
e | Float | single-precision, little-endian byte order
G | Float | double-precision, network (big-endian) byte order
g | Float | single-precision, network (big-endian) byte order
String | |
Directive | | Meaning
---------------------------------------------------------------------------
A | String | arbitrary binary string (space padded, count is width)
a | String | arbitrary binary string (null padded, count is width)
Z | String | same as ``a'', except that null is added with *
B | String | bit string (MSB first)
b | String | bit string (LSB first)
H | String | hex string (high nibble first)
h | String | hex string (low nibble first)
u | String | UU-encoded string
M | String | quoted printable, MIME encoding (see RFC2045)
m | String | base64 encoded string (see RFC 2045, count is width)
| | (if count is 0, no line feed are added, see RFC 4648)
P | String | pointer to a structure (fixed-length string)
p | String | pointer to a null-terminated string
Misc. | |
Directive | | Meaning
---------------------------------------------------------------------------
@ | --- | moves to absolute position
X | --- | back up a byte
x | --- | null byte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment