https://stackoverflow.com/questions/17623668/what-does-string-packh-mean編碼
It interprets the string as hex numbers, two characters per byte, and converts it to a string with the characters with the corresponding ASCII code:code
["464F4F"].pack('H*') # => "FOO", 0x46 is the code for 'F', 0x4F the code for 'O'
把這個字符串當成是16進制,並且每兩個字符當成是一個數,正好好一個字節,按照ASCII編碼。字符串
For the opposite conversion, use unpack
:get
'FOO'.unpack('H*') # => ["464f4f"]
It is a little bit more difficult for non-ASCII-8BIT encodings:string
"á".encoding # => #<Encoding:UTF-8> "á".unpack('H*') # => ["c3a1"] ['c3a1'].pack('H*') # => "\xC3\xA1" ['c3a1'].pack('H*').encoding # => #<Encoding:ASCII-8BIT> ['c3a1'].pack('H*').force_encoding('UTF-8') # => "á"