Forum: Ruby Unescaping hex encoded characters in string?

Posted by sprite (Guest)
on 2010-09-02 07:45
(Received via mailing list)
If I have a string like "Don\x{2019}t Let Me Fall"

How do I unescape the hex encoded characters, in this cae convert the
string to "Don't Let Me Fall"
Posted by Robert Klemme (Guest)
on 2010-09-02 09:29
(Received via mailing list)
On Thu, Sep 2, 2010 at 7:45 AM, sprite <aake.gregertsen@gmail.com> 
wrote:
> If I have a string like "Don\x{2019}t Let Me Fall"
>
> How do I unescape the hex encoded characters, in this cae convert the
> string to "Don't Let Me Fall"

In Ruby 1.9.* something along the lines of

str.gsub /\\x\{(\h{4})\}/ do |code|
  code.to_i(16).chr(str.encoding)
  # code.to_i(16).chr(Encoding::UTF_8)
end

Or use gsub! for inplace modification of the string.  You might have
to twiddle with the encodings.  Please see
http://blog.grayproductions.net/articles/miscellaneous_m17n_details

Kind regards

robert
Posted by sprite (Guest)
on 2010-09-02 23:01
(Received via mailing list)
On Sep 2, 3:26 am, Robert Klemme <shortcut...@googlemail.com> wrote:
>   # code.to_i(16).chr(Encoding::UTF_8)
> remember.guy do |as, often| as.you_can - without endhttp://blog.rubybestpractices.com/
Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.