unpacking network long that contain ints

Discussion in 'Ruby' started by Joe Van Dyk, Apr 2, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    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
     
    Joe Van Dyk, Apr 2, 2005
    #1
    1. Advertising

  2. In Message-Id: <>
    Joe Van Dyk <> 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?


    --
    April 3, 2005
    Better late than never.
     
    YANAGAWA Kazuhisa, Apr 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris

    unpacking ints

    Chris, Mar 1, 2004, in forum: C++
    Replies:
    5
    Views:
    393
    Karl Heinz Buchegger
    Mar 2, 2004
  2. Replies:
    3
    Views:
    607
    Mark P
    Apr 3, 2005
  3. Chris N. Hinds

    unions with long long ints and doubles?

    Chris N. Hinds, Sep 30, 2003, in forum: C Programming
    Replies:
    3
    Views:
    436
    Barry Schwarz
    Oct 2, 2003
  4. Skybuck Flying

    ints ints ints and ints

    Skybuck Flying, Jul 8, 2004, in forum: C Programming
    Replies:
    24
    Views:
    870
    Jack Klein
    Jul 10, 2004
  5. Ray Dillinger

    format specifier for long long ints....

    Ray Dillinger, Mar 26, 2006, in forum: C Programming
    Replies:
    4
    Views:
    506
    Keith Thompson
    Mar 27, 2006
Loading...

Share This Page