Ruby's pack and unpack explained

后端 未结 4 547
长发绾君心
长发绾君心 2021-02-08 04:40

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 t

4条回答
  •  我在风中等你
    2021-02-08 05:24

    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"]
    

提交回复
热议问题