ruby pack unpack question
> [65].pack('c')
=> "A"
In the above case 65
is stored as 1000 0010
. Since c
returns 8-bit unsigned char
the returned value is 65
which happens to be the ASCII code for A
.
I do not get the result of follo开发者_开发百科wing two operations. Any explanations would help.
> ['A'].pack('H')
=> "\xA0"
> ['A'].pack('h')
=> "\n"
If you read the documentation you find this:
H | String | hex string (high nibble first)
h | String | hex string (low nibble first)
I don't know why you'd need to flip nibbles, but the facility is there if you need it.
In your case A
is being interpreted as A0
one way, and 0A
in the other. Hex input should be provided as character pairs.
精彩评论