Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby's String#hex confusion

Tags:

ruby

hex

I've found it weird that String#hex in Ruby doesn't return the right hex value for a given char. I might be misunderstanding the method, but take the following example:

'a'.hex
=> 10

Whereas the right hex value for 'a' would be 61:

'a'.unpack('H*')
=> 61

Am I missing something? What's hex for? Any hints appreciated!

Thanks

like image 457
FullOfCaffeine Avatar asked Jul 12 '13 00:07

FullOfCaffeine


People also ask

What is a Ruby string?

In Ruby, string is a sequence of one or more characters. It may consist of numbers, letters, or symbols. Here strings are the objects, and apart from other languages, strings are mutable, i.e. strings can be changed in place instead of creating new strings.

How do you get a string in Ruby?

Strings exist within either single quotes ' or double quotes " in Ruby, so to create a string, enclose a sequence of characters in one or the other: 'This is a string in single quotes. ' "This is a string in double quotes." You can choose to use either single quotes or double quotes.

How do I use a string in Ruby?

A string in Ruby is an object (like most things in Ruby). You can create a string with either String::new or as literal (i.e. with the double quotes "" ). But you can also create string with the special %() syntax With the percent sign syntax, the delimiters can be any special character.


1 Answers

String#hex doesn't give you the ASCII index of a character, it's for transforming a base-16 number (hexadecimal) 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

So it uses the normal mapping:

'0'.hex #=> 0
'1'.hex #=> 1
...
'9'.hex #=> 9
'a'.hex #=> 10 == 0xA
'b'.hex #=> 11
...
'f'.hex   #=> 15 == 0xF == 0x0F
'10'.hex  #=> 16 == 0x10
'11'.hex  #=> 17 == 0x11
...
'ff'.hex  #=> 255 == 0xFF

It's very similar to String#to_i when using base 16:

'0xff'.to_i(16) #=> 255
'FF'.to_i(16)   #=> 255
'-FF'.to_i(16)  #=> -255

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
like image 165
rampion Avatar answered Oct 06 '22 06:10

rampion