unpacking network long that contain ints

J

Joe Van Dyk

Hi,

I have a binary file that contains a bunch of unsigned longs in
network byte order. Each long contains 4 unsigned ints.

In C, I'd do:
rgb_long = ntohl(rgb_long);

/* Extract out the red, green, and blue elements from the
* RGB long. */
red = rgb_long << 0 >> 24;
green = rgb_long << 8 >> 24;
blue = rgb_long << 16 >> 24;

In Ruby, I have:
# I have the long in host byte order, but it's now a FixNum.
rgb = binary_data.unpack("N")[0]

# Works, but byte order is messed up
r, g, b = binary_data.unpack("CCCC")

Ideas?

Thanks,
joe
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
Joe Van Dyk said:
# Works, but byte order is messed up
r, g, b = binary_data.unpack("CCCC")

irb(main):011:0> [0x123456].pack('N').unpack('C4').map {|e| '%x'%e}
=> ["0", "12", "34", "56"]

What do you mean by "messed up"? or do I missed something obvious?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with Loop 0
Extracting ints from a unsigned long 6
unpacking ints 5
Reading bits off an unsiged long 4
Processing byte arrays 5
VHDL help. 4
[SUMMARY] SimFrost (#117) 0
How to use htonl in IPv6 2

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top