I would like +1 dylanfm and AShelly to comment, but add [0]:
'A'.unpack('C')[0]
An open call returns an array containing a single integer, which is not always accepted where an integer is required:
$ ruby ββ-e 'printf ("0x% 02X \ n", "A" .unpack ("C"))'
-e: 1: in `printf ': can't convert Array into Integer (TypeError)
from -e: 1
$ ruby ββ-e 'printf ("0x% 02X \ n", "A" .unpack ("C") [0])'
0x41
$
I am trying to write code that works on Ruby 1.8.1, 1.8.7 and 1.9.2.
Edited to pass C for unpacking in upper case, because unpack ("c") gives me -1, where ord () gives me 255 (even though on the platform that C char is signed to).
Martin Dorey May 27 '11 at 1:34 a.m. 2011-05-27 01:34
source share