Ruby Coding Percentage
In Ruby, I get percent encoding "ä" on
require 'cgi'
CGI.escape('ä')
=> "%C3%A4"
The same with
'ä'.unpack('H2' * 'ä'.bytesize)
=> ["c3", "a4"]
I have two questions:
-
What is the opposite of the first operation? Must not be
["c3", "a4"].pack('H2' * 'ä'.bytesize) => "\xC3\xA4"
-
For my application, I need "ä" to be encoded as "% E4" which is the hexadecimal value of "ä'.ord". Is there a Ruby method for it?
source to share
As I mentioned in my comment, equating the character ä as code point 228 (0xE4) implies that you are dealing with ISO 8859-1 character encoding .
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')
Then you can code it as you like:
require 'cgi'
s2c = CGI.escape str2
#=> "Hullo+%E4ngstrom"
require 'uri'
s2u = URI.escape str2
#=> "Hullo%20%E4ngstrom"
Then, to reverse it, you must first (a) reverse the value and then (b) turn the encoding back to what you are used to (probably UTF-8), tell Ruby what character encoding it should interpret the codes in like :
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"
source to share