# String Manipulation Problems (decoding hex)

**URL:** <https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438>\
**Category:** Ruby\
**Created:** [January 28, 2010, 2:42am UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438 "2010-01-28T02:42:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![oreoshake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@oreoshake](https://www.ruby-forum.com/u/oreoshake)\
**Post date:** [January 28, 2010, 2:42am UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438/1 "2010-01-28T02:42:19Z")

</div>

First, I’m a java guy. I have a file that contains hex (e.g.  
890dfbcb…) and I’m trying to pass that to an OpenSSL::Cipher object as  
the key/iv.

For the sake of the question, I’m going to cut the key down in size

In Java I’d do the following (using apache commons codec library)

Hex hex = new Hex() // why these methods aren’t static, i’ll never know  
key = “890dfbcb”;  
hex.decode(key.getBytes());

(Trying to include as much as possible due to my n00b status)

The value of key.getBytes is  
[56, 57, 48, 100, 102, 98, 99, 98]  
The value of hex.decode(key.getBytes()) is  
[-119, 13, -5, -53]

In ruby:

key.unpack(“H\*”)[0] = 3839306466626362  
key.hex = 2299395019

When I call  
cipher = OpenSSL::Cipher::Cipher.new  
key = cipher.random\_key

key is a bunch of unicode/binary data.

I’m definitely losing something in the translation here.

---

<div class="post-metadata">

**Author:** ![oreoshake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@oreoshake](https://www.ruby-forum.com/u/oreoshake)\
**Post date:** [January 28, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438/2 "2010-01-28T18:56:38Z")

</div>

> key = cipher.random\_key
> 
> key is a bunch of unicode/binary data.

You seem to be reassigning key?

-r

---

<div class="post-metadata">

**Author:** ![oreoshake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@oreoshake](https://www.ruby-forum.com/u/oreoshake)\
**Post date:** [January 28, 2010, 7:02pm UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438/3 "2010-01-28T19:02:35Z")

</div>

Roger P. wrote:

> You seem to be reassigning key?

I’m using the output of another application (Java) that generates the  
key/IV and stores it as hex.

---

<div class="post-metadata">

**Author:** ![oreoshake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@oreoshake](https://www.ruby-forum.com/u/oreoshake)\
**Post date:** [January 28, 2010, 9:26pm UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438/4 "2010-01-28T21:26:12Z")

</div>

Roger P. wrote:

> > I’m using the output of another application (Java) that generates the  
> > key/IV and stores it as hex.
> 
> [http://snippets.dzone.com/posts/show/576](http://snippets.dzone.com/posts/show/576)  
> perhaps?

Thanks roger, that got me on the right path but the methods suggested  
did not work. Luckily I came across this post which is almost what I  
had in the beginning:

[http://rubyforge.org/tracker/index.php?func=detail&aid=25756&group\_id=4134&atid=15864](http://rubyforge.org/tracker/index.php?func=detail&aid=25756&group_id=4134&atid=15864)

basically all I had to do is:

[key].pack(‘H\*’)

---

<div class="post-metadata">

**Author:** ![oreoshake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/bc79bd/32.png) [@oreoshake](https://www.ruby-forum.com/u/oreoshake)\
**Post date:** [January 28, 2010, 7:17pm UTC](https://www.ruby-forum.com/t/string-manipulation-problems-decoding-hex/182438/5 "2010-01-28T19:17:09Z")

</div>

> I’m using the output of another application (Java) that generates the  
> key/IV and stores it as hex.

[http://snippets.dzone.com/posts/show/576](http://snippets.dzone.com/posts/show/576)  
perhaps?
