Generate a hex string from integers

Hi there,

I'm new in Ruby. So, my question can seem simple for you. I need to
generate a hex string from some digits: 1, 114, 3, 2 for example. I need
the following hex representation: 00010072030002 - (0x0001, 0x0072,
0x03, 0x0002).
I tried to use pack:

a = [1, 114, 3, 2]
hexStr = a.pack('iisi')

but it generate following string: 10007200302000

Also, what is the best way to convert this hex string into digits back?

Thanks in advance.

Michael

···

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

sprintf to the rescue!

   irb(main):002:0> sprintf "%04x"*4, 1, 114, 3, 2
   => "0001007200030002"

-- fxn

···

On Jun 17, 2006, at 20:52, Michael stepanov wrote:

I'm new in Ruby. So, my question can seem simple for you. I need to
generate a hex string from some digits: 1, 114, 3, 2 for example. I need
the following hex representation: 00010072030002 - (0x0001, 0x0072,
0x03, 0x0002).

Hope that helps
Robert

···

On 6/17/06, Michael stepanov <stepanov.michael@gmail.com> wrote:

Hi there,

I'm new in Ruby. So, my question can seem simple for you. I need to
generate a hex string from some digits: 1, 114, 3, 2 for example. I need
the following hex representation: 00010072030002 - (0x0001, 0x0072,
0x03, 0x0002).
I tried to use pack:

a = [1, 114, 3, 2]
hexStr = a.pack('iisi')

but it generate following string: 10007200302000

Also, what is the best way to convert this hex string into digits back?

Thanks in advance.

Michael

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

[1, 114, 3, 2].map{|n| "%04x" %n}.join

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Not a bad idea, but that won't exactly do it: he wants only one octet for the 3.

Try this:

hex_str = a.pack('nnCn').unpack('H*')[0] # => "00010072030002"

Paul.

···

On 17/06/06, Xavier Noria <fxn@hashref.com> wrote:

On Jun 17, 2006, at 20:52, Michael stepanov wrote:

> I'm new in Ruby. So, my question can seem simple for you. I need to
> generate a hex string from some digits: 1, 114, 3, 2 for example. I
> need
> the following hex representation: 00010072030002 - (0x0001, 0x0072,
> 0x03, 0x0002).

sprintf to the rescue!

   irb(main):002:0> sprintf "%04x"*4, 1, 114, 3, 2
   => "0001007200030002"

A bit more efficient

irb(main):005:0> a.inject("") {|s,i| s << "%04x" % i}
=> "0001007200030002"

Kind regards

robert

···

2006/6/17, Robert Dober <robert.dober@gmail.com>:

> [1, 114, 3, 2].map{|n| "%04x" %n}.join

--
Have a look: Robert K. | Flickr

Oh yes, I didn't note the 3 was different (I guess it is different from the rest because of the semantics of the wanted string). With sprintf that would be

   sprintf "%04x%04x%02x%04x", 1, 114, 3, 2

-- fxn

···

On Jun 17, 2006, at 21:58, Paul Battley wrote:

On 17/06/06, Xavier Noria <fxn@hashref.com> wrote:

On Jun 17, 2006, at 20:52, Michael stepanov wrote:

> I'm new in Ruby. So, my question can seem simple for you. I need to
> generate a hex string from some digits: 1, 114, 3, 2 for example. I
> need
> the following hex representation: 00010072030002 - (0x0001, 0x0072,
> 0x03, 0x0002).

sprintf to the rescue!

   irb(main):002:0> sprintf "%04x"*4, 1, 114, 3, 2
   => "0001007200030002"

Not a bad idea, but that won't exactly do it: he wants only one octet for the 3.

Robert Klemme wrote:

···

2006/6/17, Robert Dober <robert.dober@gmail.com>:

> [1, 114, 3, 2].map{|n| "%04x" %n}.join

A bit more efficient

irb(main):005:0> a.inject("") {|s,i| s << "%04x" % i}
=> "0001007200030002"

Thanks a lot, guys, for your answers. You helped me a lot!

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