What is the opposite of chr () in Ruby?

Many languages ​​have a pair of chr() and ord() functions that convert between numbers and symbolic values. In some languages, ord() is called asc() .

Ruby has Integer#chr , which works fine:

 >> 65.chr A 

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

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

prints:

 65 

and it's pretty close to what i want. But I would rather avoid a loop - I'm looking for something short enough to be readable when const declared.

+84
ruby ascii
Nov 21 '08 at 13:27
source share
10 answers

If String # ord does not exist in 1.9, then this happens in 2.0:

 "A".ord #=> 65 
+68
Dec 10 '13 at 17:50
source share

In Ruby prior to version 1.8, including the following: both will produce 65 (for ASCII):

 puts ?A 'A'[0] 

The behavior has changed in Ruby 1.9, both of the above produce instead an β€œA”. The correct way to do this in Ruby 1.9 is:

 'A'[0].ord 

Unfortunately, the ord method does not exist in Ruby 1.8.

+33
Nov 21 '08 at 14:19
source share

Try:

 'A'.unpack('c') 
+13
Nov 21 '08 at 13:44
source share

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).

+10
May 27 '11 at 1:34
source share

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

 puts 'Az'[0] => 65 puts 'Az'[1] => 122 
+3
Nov 21 '08 at 13:45
source share

Just stumbled upon this while building a clean version of Ruby Ruby from Stringprep via RFC.

Beware that chr goes beyond [0.255], use the portable replacements 1.9.x - 2.1.x instead;

 [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)> 
+3
Apr 05 '14 at 5:51 on
source share

What about

puts? A

+2
Nov 21 '08 at 13:32
source share

If you don't mind pulling values ​​from an array, you can use "A".bytes

+2
May 24 '14 at 21:51
source share

You may have the following:

 65.chr.ord 'a'.ord.chr 
+1
Mar 10 '14 at 19:38
source share

I am writing code for 1.8.6 and 1.9.3, and I could not 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

This did not work for me either, but I adapted it for use:

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

Having done this, the following will work in both environments:

 'A'[0].ord 
0
Apr 13 '15 at 15:11
source share



All Articles