1.9 inverse of String#ord

B

Brian Candler

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

James Gray

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

This is the best I can come up with:
cp =3D "=C5=82".ord =3D> 322
[cp].pack("N").force_encoding("UTF-32BE").encode!("UTF-8")
=3D> "=C5=82"

Note that Integer#chr only works up to 255.

Actually, it can take an encoding now:

irb(main):001:0> 322.chr("UTF-8")
=3D> "=C5=82"

James Edward Gray II=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,281
Latest member
Pedroaciny

Latest Threads

Top