Un-escaping hexadecimal code

Hi, I receive a String allowing hexadecimal escaping by using %XX syntax:

  %61lice => alice ( %61 == a )

I would like to un-escape the string. I've got it using "eval" but I'd prefer
avoiding using "eval":

  string = "%61lice"
  string = eval %{ "#{ string.gsub(/%/,'\x') }" }
  => "alice"

How could I avoid the usage of "eval" to un-escape the string?

Thanks a lot.

···

--
Iñaki Baz Castillo

Ops, it is easier than above. CGI lib already does it:

  require 'cgi'
  CGI.unescape("%61lice")
  => "alice"

:slight_smile:

···

El Viernes, 20 de Febrero de 2009, Iñaki Baz Castillo escribió:

Hi, I receive a String allowing hexadecimal escaping by using %XX syntax:

  %61lice => alice ( %61 == a )

I would like to un-escape the string. I've got it using "eval" but I'd
prefer avoiding using "eval":

  string = "%61lice"
  string = eval %{ "#{ string.gsub(/%/,'\x') }" }
  => "alice"

How could I avoid the usage of "eval" to un-escape the string?

--
Iñaki Baz Castillo

Iñaki Baz Castillo wrote:

  string = "%61lice"
  string = eval %{ "#{ string.gsub(/%/,'\x') }" }
  => "alice"

How could I avoid the usage of "eval" to un-escape the string?

s.gsub! /%(\d+)/ do |match|
   $1.to_i(16).chr
end
puts s

You might also consider supporting escaped % literals.

It fails since it should be:

  s.gsub! /%(\d\d)/ do |match|
    $1.to_i(16).chr
  end

Without this change it would fail when matching:

  "%61123"

But fixing it then it works well, thanks a lot :slight_smile:

···

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:

Iñaki Baz Castillo wrote:
> string = "%61lice"
> string = eval %{ "#{ string.gsub(/%/,'\x') }" }
> => "alice"
>
> How could I avoid the usage of "eval" to un-escape the string?

s.gsub! /%(\d+)/ do |match|
   $1.to_i(16).chr
end
puts s

You might also consider supporting escaped % literals.

--
Iñaki Baz Castillo

Also, it should consider not just number but A-F

···

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:

Iñaki Baz Castillo wrote:
> string = "%61lice"
> string = eval %{ "#{ string.gsub(/%/,'\x') }" }
> => "alice"
>
> How could I avoid the usage of "eval" to un-escape the string?

s.gsub! /%(\d+)/ do |match|
   $1.to_i(16).chr
end
puts s

You might also consider supporting escaped % literals.

--
Iñaki Baz Castillo

Iñaki Baz Castillo wrote:

···

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:

Iñaki Baz Castillo wrote:

  string = "%61lice"
  string = eval %{ "#{ string.gsub(/%/,'\x') }" }
  => "alice"

How could I avoid the usage of "eval" to un-escape the string?

s.gsub! /%(\d+)/ do |match|
   $1.to_i(16).chr
end
puts s

You might also consider supporting escaped % literals.

Also, it should consider not just number but A-F

Right you are.

Iñaki Baz Castillo wrote:

···

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:

puts s

You might also consider supporting escaped % literals.

Also, it should consider not just number but A-F

s = "%4Flice"
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
puts s
--
Posted via http://www.ruby-forum.com/\.

Igor Pirnovar wrote:

Iñaki Baz Castillo wrote:

puts s

You might also consider supporting escaped % literals.

Also, it should consider not just number but A-F

s = "%4Flice"
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
puts s

SyntaxError: compile error
(irb):2: syntax error, unexpected '{', expecting $end
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
                                    ^

···

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribió:

Jeff Schwab wrote:

SyntaxError: compile error
(irb):2: syntax error, unexpected '{', expecting $end
s.gsub! /%([\da-fA-F][\da-fA-F])/ { $1.to_i(16).chr }
                                    ^

Sorry, in Ruby 1.9 this is no longer a Syntax Error

···

--
Posted via http://www.ruby-forum.com/\.