Convert characters from hex to string

S

Shandy Nantz

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
 
F

Florian Gilcher

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

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&userlogin=shhhhh@
\n"

==== code ====

Greetings
Florian Gilcher
 
C

Chris Hulan

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

Is there a way to do this? Thanks,

-S

FYI
irb(main):003:0> CGI.unescape 'http://www.example.com/logic?ss
%3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/'
=> "http://www.example.com/logic?
ss=1&companyname=shhhhhhh&userlogin=shhhhh@/"

cheers
 
S

Shandy Nantz

Chris said:
FYI
irb(main):003:0> CGI.unescape 'http://www.example.com/logic?ss
%3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/'
=> "http://www.example.com/logic?
ss=1&companyname=shhhhhhh&userlogin=shhhhh@/"

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
 
R

Rick DeNatale

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.

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?
 
S

Shandy Nantz

Rick said:
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
 
F

Florian Gilcher

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

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 Gilcher
 
S

Shandy Nantz

Florian said:
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 Gilcher

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

Rick DeNatale

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.
 

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,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top