What's the opposite of chr() in Ruby?

RubyAscii

Ruby Problem Overview


In many languages there's a pair of functions, chr() and ord(), which convert between numbers and character values. In some languages, ord() is called asc().

Ruby has Integer#chr, which works great:

>> 65.chr
A

Fair enough. But how do you go the other way?

"A".each_byte do |byte|
   puts byte
end

prints:

65

and that's pretty close to what I want. But I'd really rather avoid a loop -- I'm looking for something short enough to be readable when declaring a const.

Ruby Solutions


Solution 1 - Ruby

If String#ord didn't exist in 1.9, it does in 2.0:

"A".ord #=> 65

Solution 2 - Ruby

In Ruby up to and including the 1.8 series, the following will both produce 65 (for ASCII):

puts ?A
'A'[0]

The behavior has changed in Ruby 1.9, both of the above will produce "A" instead. The correct way to do this in Ruby 1.9 is:

'A'[0].ord

Unfortunately, the ord method doesn't exist in Ruby 1.8.

Solution 3 - Ruby

Try:

'A'.unpack('c')

Solution 4 - Ruby

I'd like to +1 dylanfm and AShelly's comment but add the [0]:

'A'.unpack('C')[0]

The unpack call returns an Array containing a single integer, which is not always accepted where an integer is wanted:

$ 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'm trying to write code that works on Ruby 1.8.1, 1.8.7 and 1.9.2.

Edited to pass C to unpack in uppercase, because unpack("c") gives me -1 where ord() gives me 255 (despite running on a platform where C's char is signed).

Solution 5 - Ruby

Just came across this while putting together a pure Ruby version of Stringprep via RFCs.

Beware that chr fails outside [0,255], instead use 1.9.x - 2.1.x portable replacements:

[22] pry(main)> "\u0221".ord.chr
RangeError: 545 out of char range
from (pry):2:in 'chr'
[23] pry(main)> x = "\u0221".unpack('U')[0]
=> 545
[24] pry(main)> [x].pack('U')
=> "ȡ"
[25] pry(main)>

Solution 6 - Ruby

Additionally, if you have the char in a string and you want to decode it without a loop:

puts 'Az'[0]
=> 65
puts 'Az'[1]
=> 122

Solution 7 - Ruby

How about

> puts ?A

Solution 8 - Ruby

You can have these:

65.chr.ord
'a'.ord.chr

Solution 9 - Ruby

If you don't mind pulling the values out of an array, you can use "A".bytes

Solution 10 - Ruby

I'm writing code for 1.8.6 and 1.9.3 and I couldn't get any of these solutions to work in both environments :(

However, I came across another solution: http://smajnr.net/2009/12/ruby-1-8-nomethoderror-undefined-method-ord-for-string.html

That didn't work for me either but I adapted it for my use:

unless "".respond_to?(:ord)
  class Fixnum
    def ord
      return self
    end
  end
end

Having done that, then the following will work in both environments

'A'[0].ord

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRJHunterView Question on Stackoverflow
Solution 1 - RubyRob CameronView Answer on Stackoverflow
Solution 2 - RubyRobert GambleView Answer on Stackoverflow
Solution 3 - RubydylanfmView Answer on Stackoverflow
Solution 4 - RubyMartin DoreyView Answer on Stackoverflow
Solution 5 - Rubyuser246672View Answer on Stackoverflow
Solution 6 - RubyKent FredricView Answer on Stackoverflow
Solution 7 - RubyGregDView Answer on Stackoverflow
Solution 8 - RubyEduardo SantanaView Answer on Stackoverflow
Solution 9 - RubyClarkView Answer on Stackoverflow
Solution 10 - RubyhantscolinView Answer on Stackoverflow