Ruby 中的百分比编码 [英] Percent encoding in Ruby
问题描述
在 Ruby 中,我得到了 'ä' 的百分比编码
In Ruby, I get the percent-encoding of 'ä' by
require 'cgi'
CGI.escape('ä')
=> "%C3%A4"
同
'ä'.unpack('H2' * 'ä'.bytesize)
=> ["c3", "a4"]
我有两个问题:
第一个操作的逆向是什么?不应该是
What is the reverse of the first operation? Shouldn't it be
["c3", "a4"].pack('H2' * 'ä'.bytesize)
=> "\xC3\xA4"
对于我的应用程序,我需要将 'ä' 编码为%E4",这是 'ä'.ord 的十六进制值.有没有针对它的 Ruby 方法?
For my application I need 'ä' to be encoded as "%E4" which is the hex-value of 'ä'.ord. Is there any Ruby-method for it?
推荐答案
正如我在评论中提到的,将字符 ä 等同于代码点 228 (0xE4) 意味着您正在处理 ISO 8859-1 字符编码.
As I mentioned in my comment, equating the character ä as the codepoint 228 (0xE4) implies that you're dealing with the ISO 8859-1 character encoding.
因此,您需要告诉 Ruby 您想要的字符串编码.
So, you need to tell Ruby what encoding you want for your string.
str1 = "Hullo ängstrom" # uses whatever encoding is current, generally utf-8
str2 = str1.encode('iso-8859-1')
然后你可以随意编码:
require 'cgi'
s2c = CGI.escape str2
#=> "Hullo+%E4ngstrom"
require 'uri'
s2u = URI.escape str2
#=> "Hullo%20%E4ngstrom"
然后,要反转它,您必须首先 (a) 取消转义值,然后 (b) 将编码转换回您习惯的编码(可能是 UTF-8),告诉 Ruby 什么字符编码它应该将代码点解释为:
Then, to reverse it, you must first (a) unescape the value, and then (b) turn the encoding back into what you're used to (likely UTF-8), telling Ruby what character encoding it should interpret the codepoints as:
s3a = CGI.unescape(s2c) #=> "Hullo \xE4ngstrom"
puts s3a.encode('utf-8','iso-8859-1')
#=> "Hullo ängstrom"
s3b = URI.unescape(s2u) #=> "Hullo \xE4ngstrom"
puts s3b.encode('utf-8','iso-8859-1')
#=> "Hullo ängstrom"
这篇关于Ruby 中的百分比编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!