[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Convert characters from hex to string

Shandy Nantz

2/29/2008 6:07:00 PM

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
--
Posted via http://www.ruby-....

8 Answers

Florian Gilcher

2/29/2008 6:11:00 PM

0


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

> 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
> --
> Posted via http://www.ruby-....
>

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&userlog...
\n"

==== code ====

Greetings
Florian Gilcher

Chris Hulan

2/29/2008 6:13:00 PM

0

On Feb 29, 1:06 pm, Shandy Nantz <shandyb...@yahoo.com> wrote:
> 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
> --
> Posted viahttp://www.ruby-....

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

cheers

Shandy Nantz

2/29/2008 6:40:00 PM

0

Chris Hulan wrote:
> On Feb 29, 1:06 pm, Shandy Nantz <shandyb...@yahoo.com> wrote:
>> -S
>> --
>> Posted viahttp://www.ruby-....
>
> FYI
> irb(main):003:0> CGI.unescape 'http://www.example.com%2f...
> %3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/'
> => "http://www.example....
> 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
--
Posted via http://www.ruby-....

Rick DeNatale

2/29/2008 7:17:00 PM

0

On 2/29/08, Shandy Nantz <shandybleu@yahoo.com> wrote:
> Chris Hulan wrote:
> > On Feb 29, 1:06 pm, Shandy Nantz <shandyb...@yahoo.com> wrote:
>
> >> -S
> >> --
> >> Posted viahttp://www.ruby-....
> >
> > FYI
> > irb(main):003:0> CGI.unescape 'http://www.example.com%2f...
> > %3d1%26companyname%3dshhhhhhh%26userlogin%3dshhhhh%40/'
> > => "http://www.example....
> > 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.

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

Shandy Nantz

2/29/2008 8:08:00 PM

0

Rick Denatale wrote:
> On 2/29/08, Shandy Nantz <shandybleu@yahoo.com> wrote:
>> > => "http://www.example....
>> 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.denh...

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
--
Posted via http://www.ruby-....

Florian Gilcher

2/29/2008 8:21:00 PM

0


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

> Rick Denatale wrote:
>> On 2/29/08, Shandy Nantz <shandybleu@yahoo.com> wrote:
>>>> => "http://www.example....
>>> 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.denh...
>
> 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
> --
> Posted via http://www.ruby-....
>

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

Shandy Nantz

2/29/2008 8:28:00 PM

0

Florian Gilcher wrote:
> On Feb 29, 2008, at 9:08 PM, Shandy Nantz 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 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.
--
Posted via http://www.ruby-....

Rick DeNatale

2/29/2008 10:00:00 PM

0

On 2/29/08, Shandy Nantz <shandybleu@yahoo.com> wrote:
> Florian Gilcher wrote:
> > On Feb 29, 2008, at 9:08 PM, Shandy Nantz 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 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.

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