Hex to 4-byte/32-bit quantity

I’m parsing a binary file and it contains (reading the specs on the
format) uInt32Number, 4-byte/32-bit quantities.

On the specific positions I read the value 0x000a - how do I turn this
into a 32-bit quantity?

Cheers
Mattias

Instead of converting the bytes to hex first I just unpacked them:

s.unpack(“N”)

10

Mattias B. wrote:

I’m parsing a binary file and it contains (reading the specs on the
format) uInt32Number, 4-byte/32-bit quantities.

You might find this library of mine useful:

http://raa.ruby-lang.org/project/bit-struct

On Sun, Feb 25, 2007 at 09:04:44PM +0900, Mattias B. wrote:

I’m parsing a binary file and it contains (reading the specs on the
format) uInt32Number, 4-byte/32-bit quantities.

On the specific positions I read the value 0x000a - how do I turn this
into a 32-bit quantity?

0x00 0x0a is only 16 bits. Assuming you mean 0x00 0x00 0x00 0x0a:

irb(main):001:0> s = “\x00\x00\x00\x0a”
=> “\000\000\000\n”
irb(main):002:0> puts s.unpack(“N”)[0]
10
=> nil

0x00 0x0a is only 16 bits. Assuming you mean 0x00 0x00 0x00 0x0a:

Yes - this was what I ment.

Thanks

You might find this library of mine useful:

http://raa.ruby-lang.org/project/bit-struct

Found that link in a previous post here also. I just wanted to get the
hang of it by doing it my self.

But thanks
Mattias

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs