Convert characters from hex to string

I looked around and found some similar postings to my problem but not
exactly what I was looking for. I have a string that has some hex
characters intermingled in with a normal string. I need to convert those
character in their char equivalent. unpack seems not to work. My string
looks like this:

https%3A%2F%2Fwww.example.com%2Flogic%3Fss%3D1%26companyname%3DShhhhhhh%26userlogin%3Dshhhhh%40

Is there a way to do this? Thanks,

-S

On Feb 29, 2008, at 7:06 PM, Shandy N. wrote:

Is there a way to do this? Thanks,

-S

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

This is obviously an url-encoded string. So, the CGI-Library is you
saviour:

==== code ====

require ‘cgi’
CGI.unescape(‘https%3A%2F%2Fwww.example.com%2Flogic%3Fss
%3D1%26companyname%3DShhhhhhh%26userlogin%3Dshhhhh%40’)

=>

https://www.example.com/logic?ss=1&companyname=Shhhhhhh&[email protected]
\n”

==== code ====

Greetings
Florian G.

On Feb 29, 1:06 pm, Shandy N. [email protected] wrote:

-S

Posted viahttp://www.ruby-forum.com/.

FYI
irb(main):003:0> CGI.unescape ‘http://www.example.com%2flogic%3fss
%3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/’
=> “http://www.example.com/logic?
ss=1&companyname=shhhhhhh&[email protected]/”

cheers

Chris H. wrote:

On Feb 29, 1:06 pm, Shandy N. [email protected] wrote:

-S

Posted viahttp://www.ruby-forum.com/.

FYI
irb(main):003:0> CGI.unescape ‘http://www.example.com%2flogic%3fss
%3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/’
=> “http://www.example.com/logic?
ss=1&companyname=shhhhhhh&[email protected]/”

cheers

I should clarify here - I am using RoR to do all of this. I had made a
post in the Rails forum but knowbody was answering. As for the
clarifying part, I am building this link in my controller and then
passing it to my view to be added into a link. The reason I do this is
that in my controller I generate a hash that is used to create an access
key for each user who is logging in, which becomes park of the link
string and then is passed to the view. However, the CGI.unescape isn’t
working.

-S

Rick Denatale wrote:

On 2/29/08, Shandy N. [email protected] wrote:

=> "http://www.example.com/logic?
key for each user who is logging in, which becomes park of the link
string and then is passed to the view. However, the CGI.unescape isn’t
working.

So presumably you are building the string without escapes in the
controller. I suspect that it’s getting escaped after that, probably
in the view. How is the variable used in the view? As an argument to
some helper, like maybe h?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

It is used in a helper - link_to. What I am doing is to create a
variable to hold the link - @link. In the view I say:

<%= link_to ‘Travel’, {:action => “#{@link}”, :title => ‘Book travel
online’, :class => ‘page_links’, :target => ‘new’ %>

But the data that is held within the @link variable has characters that
are replaced with hex values.

-S

On Feb 29, 2008, at 9:08 PM, Shandy N. wrote:

in the view. How is the variable used in the view? As an argument to

You are abusing link_to. The syntax for linking to a pregenerated
location is:

<%= link_to ‘Trave’, @link, #the other arguments %>

Otherwise, you generate a route within you controller with @link
as :action parameter.
And parameters will (and have to) be escaped by link generation.

This is really much more of a question for the rails mailing list.

Greetins
Florian G.

On 2/29/08, Shandy N. [email protected] wrote:

=> "http://www.example.com/logic?
key for each user who is logging in, which becomes park of the link
string and then is passed to the view. However, the CGI.unescape isn’t
working.

So presumably you are building the string without escapes in the
controller. I suspect that it’s getting escaped after that, probably
in the view. How is the variable used in the view? As an argument to
some helper, like maybe h?


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 2/29/08, Shandy N. [email protected] wrote:

I know this has turned out to be a rails like question but the original
question is rooted within the realm of ruby. So far I have tried
CGI.unespace and h method to try and rid a string of characters that
started out as characters but were then converted to hex characters.

The h method (which is an alias for html_escape) does just the
opposite. What I was driving at in my response is that somewhere in
your view code you were doing something like using h which was ADDING
the hex characters.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Florian G. wrote:

On Feb 29, 2008, at 9:08 PM, Shandy N. wrote:

in the view. How is the variable used in the view? As an argument to

You are abusing link_to. The syntax for linking to a pregenerated
location is:

<%= link_to ‘Trave’, @link, #the other arguments %>

Otherwise, you generate a route within you controller with @link
as :action parameter.
And parameters will (and have to) be escaped by link generation.

This is really much more of a question for the rails mailing list.

Greetins
Florian G.

I know this has turned out to be a rails like question but the original
question is rooted within the realm of ruby. So far I have tried
CGI.unespace and h method to try and rid a string of characters that
started out as characters but were then converted to hex characters.