Un-escaping hexadecimal code

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

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

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

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

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

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

Thanks a lot.

=20


=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Viernes, 20 de Febrero de 2009, I=C3=B1aki Baz Castillo escribi=C3=B3:
Hi, I receive a String allowing hexadecimal escaping by using %XX syntax:

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

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

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

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

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

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

:)



=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribi=C3=B3:
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 :)


=2D-=20
I=C3=B1aki Baz Castillo
 
I

Iñaki Baz Castillo

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribi=C3=B3:
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

=2D-=20
I=C3=B1aki Baz Castillo
 
I

Igor Pirnovar

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

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
 
I

Igor Pirnovar

Jeff said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top