Unescaping hex encoded characters in string?

S

sprite

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"
 
R

Robert Klemme

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
 
S

sprite

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 seehttp://blog.grayproductions.net/articles/miscellaneous_m17n_details

Kind regards

robert

Thanks
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top