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:
- What is the logic behind those outputs?
- How can I achieve the effect I want, i.e. transforming a sequence of hexadecimal numbers to the corresponding string. Even better - given an integer n, how to transform it to a string identical to the text file that when is considered as a number (say, in a hex editor) equals n?
'H'
formats,*
isn't acting in an expected manner according to the documentation. Other format characters seem to behave correctly, so I suspect it's a bug in Ruby's use of'H*'
. – Importunacy