Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percent encoding in Ruby

In Ruby, I get the percent-encoding of 'ä' by

require 'cgi'
CGI.escape('ä')
=> "%C3%A4"

The same with

'ä'.unpack('H2' * 'ä'.bytesize)
=> ["c3", "a4"]

I have two questions:

  1. What is the reverse of the first operation? Shouldn't it be

    ["c3", "a4"].pack('H2' * 'ä'.bytesize)
    => "\xC3\xA4"
    
  2. For my application I need 'ä' to be encoded as "%E4" which is the hex-value of 'ä'.ord. Is there any Ruby-method for it?

like image 995
ewi Avatar asked Jul 09 '15 12:07

ewi


1 Answers

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.

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 encode 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) 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"
like image 94
Phrogz Avatar answered Oct 10 '22 00:10

Phrogz