String#hex does not give you the ASCII index of a character, it is intended to convert the base-16 ( hex adecimal) number from a string to an integer:
% ri String\#hex String#hex (from ruby site) ------------------------------------------------------------------------------ str.hex -> integer ------------------------------------------------------------------------------ Treats leading characters from str as a string of hexadecimal digits (with an optional sign and an optional 0x) and returns the corresponding number. Zero is returned on error. "0x0a".hex #=> 10 "-1234".hex #=> -4660 "0".hex #=> 0 "wombat".hex #=> 0
Therefore, it uses normal mapping:
'0'.hex
It is very similar to String#to_i when using base 16:
'0xff'.to_i(16)
From the docs:
% ri String\#to_i String#to_i (from ruby site) ------------------------------------------------------------------------------ str.to_i(base=10) -> integer ------------------------------------------------------------------------------ Returns the result of interpreting leading characters in str as an integer base base (between 2 and 36). Extraneous characters past the end of a valid number are ignored. If there is not a valid number at the start of str, 0 is returned. This method never raises an exception when base is valid. "12345".to_i #=> 12345 "99 red balloons".to_i #=> 99 "0a".to_i #=> 0 "0a".to_i(16) #=> 10 "hello".to_i #=> 0 "1100101".to_i(2) #=> 101 "1100101".to_i(8) #=> 294977 "1100101".to_i(10) #=> 1100101 "1100101".to_i(16) #=> 17826049
source share