1.9 inverse of String#ord

Is there a simple inverse of String#ord in ruby 1.9?

This is the best I can come up with:

cp = “Å‚”.ord
=> 322

[cp].pack(“N”).force_encoding(“UTF-32BE”).encode!(“UTF-8”)
=> “Å‚”

Note that Integer#chr only works up to 255.

On Jul 30, 2009, at 12:19 PM, Brian C. wrote:

Is there a simple inverse of String#ord in ruby 1.9?

This is the best I can come up with:

cp = “Å‚”.ord
=> 322

[cp].pack(“N”).force_encoding(“UTF-32BE”).encode!(“UTF-8”)
=> “Å‚”

Note that Integer#chr only works up to 255.

Actually, it can take an encoding now:

irb(main):001:0> 322.chr(“UTF-8”)
=> “Å‚”

James Edward G. II

James G. wrote:

Actually, it can take an encoding now:

irb(main):001:0> 322.chr(“UTF-8”)
=> “Å‚”

That’s what I was missing. Thank you.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs