Decoding a string to hexadecimal

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

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”

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 B. [email protected] wrote:

not quite so:

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


Anas Marrawi
Visit me at: www.anasonline.net

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]

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 B. [email protected] wrote:

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


Anas Marrawi
Visit me at: www.anasonline.net

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”]

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 B. [email protected] wrote:

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


Anas Marrawi
Visit me at: www.anasonline.net

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