Convert matched string of UTF-8 values to UTF-8 characters in Ruby

Posted by user1475154 on Stack Overflow See other posts from Stack Overflow or by user1475154
Published on 2012-06-22T15:12:52Z Indexed on 2012/06/22 15:16 UTC
Read the original article Hit count: 281

Filed under:
|

Trying to convert output from a rest_client GET to the characters that are represented with escape sequences.

Input: ..."sub_id":"\u0d9c\u8138\u8134\u3f30\u8139\u2b71"...

(which I put in 'all_subs')

Match: m = /sub_id\"\:\"([^\"]+)\"/.match(all_subs.to_str) [1]

Print: puts m.force_encoding("UTF-8").unpack('U*').pack('U*')

But it just comes out the same way I put it in. ie, "\u0d9c\u8138\u8134\u3f30\u8139\u2b71"

However, if I convert a raw string of it:

puts "\u0d9c\u8138\u8134\u3f30\u8139\u2b71".unpack('U*').pack('U*')

The output is perfect as "??????"

© Stack Overflow or respective owner

Related posts about ruby

Related posts about utf-8