# Hex to 4-byte/32-bit quantity

**URL:** <https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752>\
**Category:** Ruby\
**Created:** [February 25, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752 "2007-02-25T13:04:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/1 "2007-02-25T13:04:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/2 "2007-02-25T14:12:09Z")

</div>

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

s.unpack(“N”)

> 10

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 8:32pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/3 "2007-02-25T20:32:03Z")

</div>

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](http://raa.ruby-lang.org/project/bit-struct)

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/4 "2007-02-25T21:00:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/5 "2007-02-25T21:01:37Z")

</div>

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

Yes - this was what I ment.

Thanks

---

<div class="post-metadata">

**Author:** ![mattias](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cab0a1/32.png) [@mattias](https://www.ruby-forum.com/u/mattias)\
**Post date:** [February 25, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/hex-to-4-byte-32-bit-quantity/86752/6 "2007-02-25T21:40:29Z")

</div>

> You might find this library of mine useful:
> 
> [http://raa.ruby-lang.org/project/bit-struct](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
