# Decoding a string to hexadecimal

**URL:** <https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071>\
**Category:** Rails\
**Created:** [March 5, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071 "2008-03-05T20:16:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 5, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/1 "2008-03-05T20:16:08Z")

</div>

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](http://www.anasonline.net)

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 5, 2008, 8:52pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/2 "2008-03-05T20:52:58Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 6, 2008, 11:22am UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/3 "2008-03-06T11:22:31Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## not quite so:
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Anas Marrawi  
Visit me at: [www.anasonline.net](http://www.anasonline.net)

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 6, 2008, 12:25pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/4 "2008-03-06T12:25:44Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/5 "2008-03-08T14:22:47Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

–  
Anas Marrawi  
Visit me at: [www.anasonline.net](http://www.anasonline.net)

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 8, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/6 "2008-03-08T20:44:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 9, 2008, 11:59am UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/7 "2008-03-09T11:59:54Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Anas Marrawi  
Visit me at: [www.anasonline.net](http://www.anasonline.net)

---

<div class="post-metadata">

**Author:** ![ANSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@ANSS](https://www.ruby-forum.com/u/ANSS)\
**Post date:** [March 9, 2008, 12:30pm UTC](https://www.ruby-forum.com/t/decoding-a-string-to-hexadecimal/131071/8 "2008-03-09T12:30:03Z")

</div>

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](http://www.ruby-doc.org/core/classes/String.html#M000818)
