From: Phrogz Date: 2009-09-02T14:40:07+09:00 Subject: Re: how to convert string to binary and back in Ruby 1.9? On Sep 1, 6:38 pm, Joe wrote: > OK, I found the Array#pack method.  At first glance, it seemed to be > exactly what I was looking for.  I could do str.bytes.to_a to turn a > String into raw bytes, and Array#pack will turn them right back into a > String. > > But go to > > http://ruby-doc.org/core-1.9/classes/Array.html > > The method is missing from the 1.9 documentation.  Has it been > deprecated? I don't believe so. I don't know why it's not in the docs there, but it's in my local ri: Slim2:~ phrogz$ ri -T Array#pack ------------------------------------------------------------- Array#pack arr.pack ( aTemplateString ) -> aBinaryString From Ruby 1.9.1 ------------------------------------------------------------------------ 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 *