Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用utf8编码,我知道
a = "\u20ac" #=> "€"
我想知道是否有反转这个过程以从字符串€中获取“\u20ac”?
像
a.get_encoding_method #=> "\u20ac"
escaped = '\u' + a.unpack('U')[0].to_s(16)
试试'\u%x' % a.ord。(假设您想以带有文字反斜杠和的字符串结尾u)。
'\u%x' % a.ord
u
您可以使用 pack 和 unpack 来回转换。例如:
'€'.unpack('U*') => [8364] [8364].pack('U*') => "€"