Ruby Forum Ruby on Rails > Decoding a string to hexadecimal

Posted by AN@S (Guest)
on 05.03.2008 20:16
(Received via mailing list)
Hello,
I need to decode a utf-8 string to hexadecimal. After a lot of search I
found the unpack method of the string class, it can be used like:

 "f".unpack('U')[0].to_s(16)
=> "66"

This method seems to decode only one character and returns the decoded 
value
as an array. The problem is that I need to decode long strings and this
method seems inefficient when dealing with long strings.

Do you guys have any idea? I don't think I should convert my string
characters into arrays, convert them one by one, then join them as an 
array
of converted characters then convert them to a string again !!!!

What do you think?

--
Anas Marrawi
Visit me at: www.anasonline.net
Posted by Mark Bush (bush)
on 05.03.2008 20:52
AN@S wrote:
>  "f".unpack('U')[0].to_s(16)
> => "66"

You can still use unpack to do this as it's argument will translate as 
many characters from the string as there are items to correspond in the 
argument.  This is ugly when applying to a litteral, but to a variable, 
not quite so:

s = "hello"
s.unpack('U'*s.length).collect {|x| x.to_s 16}
=> ["68", "65", "6c", "6c", "6f"]

If you want all the hex digits strung together:

s.unpack('U'*s.length).collect {|x| x.to_s 16}.join
=> "68656c6c6f"
Posted by AN@S (Guest)
on 06.03.2008 11:22
(Received via mailing list)
Thank you Mark this works :), but it seems to ignore space characters, 
for
example:

"anas".unpack('U'*"anas".length)
=> [97, 110, 97, 115]

"anas is happy".unpack('U'*"anas".length)
=> [97, 110, 97, 115]

See? everything after the space is ignored and not stored as a space
character in the array? I couldn't work around this? !!

Thanks

On 05/03/2008, Mark Bush <rails-mailing-list@andreas-s.net> wrote:
> not quite so:
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Anas Marrawi
Visit me at: www.anasonline.net
Posted by Mark Bush (bush)
on 06.03.2008 12:25
AN@S wrote:
> "anas is happy".unpack('U'*"anas".length)
> => [97, 110, 97, 115]
> 
> See? everything after the space is ignored and not stored as a space
> character in the array? I couldn't work around this? !!

This is why I said it was ugly for litterals.  You mean:

"anas is happy".unpack('U'*"anas is happy".length)
=> [97, 110, 97, 115, 32, 105, 115, 32, 104, 97, 112, 112, 121]
Posted by AN@S (Guest)
on 08.03.2008 14:22
(Received via mailing list)
Hi,
It seems to get uglier now ! What if I want to exclude numbers from 
being
converted, I've been trying to do this all the day but I failed, I tried
different approaches, this is the last one:

number = /\d/
s = "hello5"
s.unpack('U'*s.length).collect {|x|
            if x !=~ number
                        x.to_s 16
            end
}

I tried more complex methods but they all failed, the result of the 
above
code is:

=> ["68", "65", "6c", "6c", "6f", "35"]

While I need to reach the following:

=> ["68", "65", "6c", "6c", "6f", "5"]

??

I've tested the regular expression in a separate code and it looks to 
work,
but when using it here ... I don't know what's the problem.

Thanks in advance for the help.

On 06/03/2008, Mark Bush <rails-mailing-list@andreas-s.net> wrote:
> This is why I said it was ugly for litterals.  You mean:
>
--
Anas Marrawi
Visit me at: www.anasonline.net
Posted by Mark Bush (bush)
on 08.03.2008 20:44
AN@S wrote:
> number = /\d/
> s = "hello5"
> s.unpack('U'*s.length).collect {|x|
>             if x !=~ number
>                         x.to_s 16
>             end
> }

The result of the unpack:

s.unpack('U'*s.length)
=> [104, 101, 108, 108, 111, 53]

They are all numbers

> While I need to reach the following:
> 
> => ["68", "65", "6c", "6c", "6f", "5"]

s.split(//).collect do |x|
  x.match(/\d/) ? x : x.unpack('U')[0].to_s(16)
end
=> ["68", "65", "6c", "6c", "6f", "5"]
Posted by AN@S (Guest)
on 09.03.2008 11:59
(Received via mailing list)
WOW, I like it

 x.match(/\d/) ? x : x.unpack('U')[0].to_s(16)

So this will check the value of x against the regex, if the value of x 
is a
digit no change is made else it will be translated into hex, is that 
right?
I have a question, what does split(//) do?

Thanks a lot for your help

On 08/03/2008, Mark Bush <rails-mailing-list@andreas-s.net> wrote:
>
>
>
>
> --
>
> Posted via http://www.ruby-forum.com/.
>
> >
>


--
Anas Marrawi
Visit me at: www.anasonline.net
Posted by Mark Bush (bush)
on 09.03.2008 12:30
AN@S wrote:
>  x.match(/\d/) ? x : x.unpack('U')[0].to_s(16)
> 
> So this will check the value of x against the regex, if the value of x 
> is a
> digit no change is made else it will be translated into hex, is that 
> right?

Yes, that's right.

> I have a question, what does split(//) do?

#split will split a string into an array of sub-strings at places which 
match the argument.  If the argument is an empty regular expression, 
then it splits at each letter.  See the documentation for more details:

http://www.ruby-doc.org/core/classes/String.html#M000818