Even after reading the standard documentation, I still can't understand how Ruby's Array#pack
and String#unpack
exactly work. Here is the example that's causing me the most trouble:
irb(main):001:0> chars = ["61","62","63"]
=> ["61", "62", "63"]
irb(main):002:0> chars.pack("H*")
=> "a"
irb(main):003:0> chars.pack("HHH")
=> "```"
I expected both these operations to return the same output: "abc". Each of them "fails" in a different manner (not really a fail since I probably expect the wrong thing). So two questions:
We were working on a similar problem this morning. If the array size is unknown, you can use:
ary = ["61", "62", "63"]
ary.pack('H2' * ary.size)
=> "abc"
You can reverse it using:
str = "abc"
str.unpack('H2' * str.size)
=> ["61", "62", "63"]