ruby - Convert a string (representing UTF-8 hex) to string -
i have string in utf-8 hex this:
s = "0059006f007500720020006300720065006400690074002000680061007300200067006f006e0065002000620065006c006f00770020003500200064006f006c006c006100720073002e00200049006600200079006f00750020006800610076006500200061006e0020004100640064002d004f006e0020006f007200200042006f006e0075007300200079006f007500720020007200650073006f00750072006300650073002000770069006c006c00200077006f0072006b00200075006e00740069006c0020006500780068006100750073007400650064002e00200054006f00200074006f00700020007500700020006e006f007700200076006900730069007400200076006f006400610066006f006e0065002e0063006f002e006e007a002f0074006f007000750070"
i want convert actual utf-8 string. should read:
your credit has gone below 5 dollars. if have add-on or bonus resources work until exhausted. top visit vodafone.co.nz/topup
this works:
s.scan(/.{4}/).map { |a| [a.hex].pack('u') }.join
but i'm wondering if there's better way this: whether should using encoding#convert.
the 00
s suggest string hex representation of utf-16 string, rather utf-8. assuming case steps need carry out utf-8 string first convert string actual bytes hex digits represents (array#pack
can used this), second mark being in appropriate encoding force_encoding
(which looks utf-16be) , use encode
convert utf-8:
[s].pack('h*').force_encoding('utf-16be').encode('utf-8')
Comments
Post a Comment