将字符串(表示 UTF-8 十六进制)转换为字符串

Convert a string (representing UTF-8 hex) to string

我有一个像这样的 UTF-8 十六进制字符串:

s = "0059006F007500720020006300720065006400690074002000680061007300200067006F006E0065002000620065006C006F00770020003500200064006F006C006C006100720073002E00200049006600200079006F00750020006800610076006500200061006E0020004100640064002D004F006E0020006F007200200042006F006E0075007300200079006F007500720020007200650073006F00750072006300650073002000770069006C006C00200077006F0072006B00200075006E00740069006C0020006500780068006100750073007400650064002E00200054006F00200074006F00700020007500700020006E006F007700200076006900730069007400200076006F006400610066006F006E0065002E0063006F002E006E007A002F0074006F007000750070"

我想将其转换为实际的 UTF-8 字符串。它应该是:

Your credit has gone below 5 dollars. If you have an Add-On or Bonus your resources will work until exhausted. To top up now visit vodafone.co.nz/topup

这个有效:

s.scan(/.{4}/).map { |a| [a.hex].pack('U') }.join

但我想知道是否有更好的方法来做到这一点:我是否应该使用 Encoding#convert.

我认为整个字符串中都有额外的空字符(这是有效的,但很浪费),但您可以尝试:

[s].pack('H*').force_encoding('utf-8')

虽然,好像"Your credit has gone below 5 dollars"...

字符串打印为puts,但转储字符串时无法在终端上读取所有unicode字符。

如果您打算在其他奇怪编码的字符串上使用它,您可以取消填充前导字节:

[s.gsub(/..(..)/,'')].pack('H*')

或使用它们:

s.gsub(/..../){|p|p.hex.chr}

如果你想使用Encoding::Converter

ec = Encoding::Converter.new('UTF-16BE','UTF-8')     # save converter for reuse
ec.convert( [s].pack('H*') )                         # or:  ec.convert [s].pack'H*'

额外的 00s 表明字符串实际上是 UTF-16 string, rather than UTF-8. Assuming that is the case the steps you need to carry out to get a UTF-8 string are first convert the string into the actual bytes the hex digits represents (Array#pack can be used for this), second mark it as being in the appropriate encoding with force_encoding (which looks like UTF-16BE) and finally use encode 的十六进制表示,将其转换为 UTF-8:

[s].pack('H*').force_encoding('utf-16be').encode('utf-8')