[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Un-escaping hexadecimal code

Iñaki Baz Castillo

2/20/2009 4:05:00 PM

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

8 Answers

Iñaki Baz Castillo

2/20/2009 4:08:00 PM

0

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

Jeff Schwab

2/20/2009 4:20:00 PM

0

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

2/20/2009 4:35:00 PM

0

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribi=C3=B3:
> I=C3=B1aki Baz Castillo wrote:
> > string =3D "%61lice"
> > string =3D eval %{ "#{ string.gsub(/%/,'\x') }" }
> > =3D> "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 :)


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

Iñaki Baz Castillo

2/20/2009 4:37:00 PM

0

El Viernes, 20 de Febrero de 2009, Jeff Schwab escribi=C3=B3:
> I=C3=B1aki Baz Castillo wrote:
> > string =3D "%61lice"
> > string =3D eval %{ "#{ string.gsub(/%/,'\x') }" }
> > =3D> "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

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

Jeff Schwab

2/20/2009 5:22:00 PM

0

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.

Igor Pirnovar

2/20/2009 6:28:00 PM

0

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-....

Jeff Schwab

2/20/2009 6:56:00 PM

0

Igor Pirnovar wrote:
> 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

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

Igor Pirnovar

2/20/2009 7:32:00 PM

0

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-....