|
1 | 1 | = Packed \Data
|
2 | 2 |
|
| 3 | +== Quick Reference |
| 4 | + |
| 5 | +These tables summarize the directives for packing and unpacking. |
| 6 | + |
| 7 | +=== For Integers |
| 8 | + |
| 9 | + Directive | Meaning |
| 10 | + --------------|--------------------------------------------------------------- |
| 11 | + C | 8-bit unsigned (unsigned char) |
| 12 | + S | 16-bit unsigned, native endian (uint16_t) |
| 13 | + L | 32-bit unsigned, native endian (uint32_t) |
| 14 | + Q | 64-bit unsigned, native endian (uint64_t) |
| 15 | + J | pointer width unsigned, native endian (uintptr_t) |
| 16 | + |
| 17 | + c | 8-bit signed (signed char) |
| 18 | + s | 16-bit signed, native endian (int16_t) |
| 19 | + l | 32-bit signed, native endian (int32_t) |
| 20 | + q | 64-bit signed, native endian (int64_t) |
| 21 | + j | pointer width signed, native endian (intptr_t) |
| 22 | + |
| 23 | + S_ S! | unsigned short, native endian |
| 24 | + I I_ I! | unsigned int, native endian |
| 25 | + L_ L! | unsigned long, native endian |
| 26 | + Q_ Q! | unsigned long long, native endian |
| 27 | + | (raises ArgumentError if the platform has no long long type) |
| 28 | + J! | uintptr_t, native endian (same with J) |
| 29 | + |
| 30 | + s_ s! | signed short, native endian |
| 31 | + i i_ i! | signed int, native endian |
| 32 | + l_ l! | signed long, native endian |
| 33 | + q_ q! | signed long long, native endian |
| 34 | + | (raises ArgumentError if the platform has no long long type) |
| 35 | + j! | intptr_t, native endian (same with j) |
| 36 | + |
| 37 | + S> s> S!> s!> | each the same as the directive without >, but big endian |
| 38 | + L> l> L!> l!> | S> is the same as n |
| 39 | + I!> i!> | L> is the same as N |
| 40 | + Q> q> Q!> q!> | |
| 41 | + J> j> J!> j!> | |
| 42 | + |
| 43 | + S< s< S!< s!< | each the same as the directive without <, but little endian |
| 44 | + L< l< L!< l!< | S< is the same as v |
| 45 | + I!< i!< | L< is the same as V |
| 46 | + Q< q< Q!< q!< | |
| 47 | + J< j< J!< j!< | |
| 48 | + |
| 49 | + n | 16-bit unsigned, network (big-endian) byte order |
| 50 | + N | 32-bit unsigned, network (big-endian) byte order |
| 51 | + v | 16-bit unsigned, VAX (little-endian) byte order |
| 52 | + V | 32-bit unsigned, VAX (little-endian) byte order |
| 53 | + |
| 54 | + U | UTF-8 character |
| 55 | + w | BER-compressed integer |
| 56 | + |
| 57 | +=== For Floats |
| 58 | + |
| 59 | + Directive | Meaning |
| 60 | + ----------|-------------------------------------------------- |
| 61 | + D d | double-precision, native format |
| 62 | + F f | single-precision, native format |
| 63 | + E | double-precision, little-endian byte order |
| 64 | + e | single-precision, little-endian byte order |
| 65 | + G | double-precision, network (big-endian) byte order |
| 66 | + g | single-precision, network (big-endian) byte order |
| 67 | + |
| 68 | +=== For Strings |
| 69 | + |
| 70 | + Directive | Meaning |
| 71 | + ----------|----------------------------------------------------------------- |
| 72 | + A | arbitrary binary string (remove trailing nulls and ASCII spaces) |
| 73 | + a | arbitrary binary string |
| 74 | + Z | null-terminated string |
| 75 | + B | bit string (MSB first) |
| 76 | + b | bit string (LSB first) |
| 77 | + H | hex string (high nibble first) |
| 78 | + h | hex string (low nibble first) |
| 79 | + u | UU-encoded string |
| 80 | + M | quoted-printable, MIME encoding (see RFC2045) |
| 81 | + m | base64 encoded string (RFC 2045) (default) |
| 82 | + | (base64 encoded string (RFC 4648) if followed by 0) |
| 83 | + P | pointer to a structure (fixed-length string) |
| 84 | + p | pointer to a null-terminated string |
| 85 | + |
| 86 | +=== Additional Directives for Packing |
| 87 | + |
| 88 | + Directive | Meaning |
| 89 | + ----------|---------------------------------------------------------------- |
| 90 | + @ | moves to absolute position |
| 91 | + X | back up a byte |
| 92 | + x | null byte |
| 93 | + |
| 94 | +=== Additional Directives for Unpacking |
| 95 | + |
| 96 | + Directive | Meaning |
| 97 | + ----------|---------------------------------------------------------------- |
| 98 | + @ | skip to the offset given by the length argument |
| 99 | + X | skip backward one byte |
| 100 | + x | skip forward one byte |
| 101 | + |
| 102 | +== Packing and Unpacking |
| 103 | + |
3 | 104 | Certain Ruby core methods deal with packing and unpacking data:
|
4 | 105 |
|
5 | 106 | - \Method Array#pack:
|
|
0 commit comments