Unpacking network long that contain ints

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

In Message-Id: <c715e64050401182020434e4c@mail.gmail.com>
Joe Van Dyk <joevandyk@gmail.com> writes:

                # 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?

···

--
kjana@dm4lab.to April 3, 2005
Better late than never.