Extracting ints from a unsigned long

Discussion in 'Ruby' started by Joe Van Dyk, Nov 18, 2004.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    I have a bunch of binary data consisting of 4 ints stored inside an unsigned
    long.

    Here's psuedo C code to extract out the ints:

    // data is a FILE pointer to the file
    unsigned long temp;
    fread(temp, sizeof(temp), 1, data);
    int first = temp >> 24;
    int second = temp << 8 >> 24;
    int third = temp << 16 >> 24;
    int fourth = temp << 24 >> 24;


    I'm trying to do the same thing in Ruby, but I'm having difficulties with
    the bitshifting. Any ideas? Would it make since to inline C code in Ruby
    for this?

    Joe
     
    Joe Van Dyk, Nov 18, 2004
    #1
    1. Advertising

  2. On Fri, Nov 19, 2004 at 07:53:19AM +0900, Joe Van Dyk wrote:
    > I have a bunch of binary data consisting of 4 ints stored inside an unsigned
    > long.
    >
    > Here's psuedo C code to extract out the ints:
    >
    > // data is a FILE pointer to the file
    > unsigned long temp;
    > fread(temp, sizeof(temp), 1, data);
    > int first = temp >> 24;
    > int second = temp << 8 >> 24;
    > int third = temp << 16 >> 24;
    > int fourth = temp << 24 >> 24;
    >
    >
    > I'm trying to do the same thing in Ruby, but I'm having difficulties with
    > the bitshifting. Any ideas? Would it make since to inline C code in Ruby
    > for this?


    str = someio.read 16
    first, second, third, fourth = str.unpack("l4")

    --
    Hassle-free packages for Ruby?
    RPA is available from http://www.rubyarchive.org/
     
    Mauricio Fernández, Nov 18, 2004
    #2
    1. Advertising

  3. Joe Van Dyk wrote:
    > I have a bunch of binary data consisting of 4 ints stored inside an unsigned
    > long.
    >
    > Here's psuedo C code to extract out the ints:
    >
    > // data is a FILE pointer to the file
    > unsigned long temp;
    > fread(temp, sizeof(temp), 1, data);
    > int first = temp >> 24;
    > int second = temp << 8 >> 24;
    > int third = temp << 16 >> 24;
    > int fourth = temp << 24 >> 24;
    >
    >
    > I'm trying to do the same thing in Ruby, but I'm having difficulties with
    > the bitshifting. Any ideas? Would it make since to inline C code in Ruby
    > for this?


    irb(main):003:0> [1,2,3,4].pack "C*"
    => "\001\002\003\004"
    irb(main):004:0> [1,2,3,4].pack("C*").unpack("C*")
    => [1, 2, 3, 4]
     
    Joel VanderWerf, Nov 18, 2004
    #3
  4. Surely they must be 4 shorts ( 16bits ) to be stored inside one long ( 64bits )?



    On Fri, 19 Nov 2004 07:53:19 +0900, Joe Van Dyk <> wrote:
    > I have a bunch of binary data consisting of 4 ints stored inside an unsigned
    > long.
    >
    > Here's psuedo C code to extract out the ints:
    >
    > // data is a FILE pointer to the file
    > unsigned long temp;
    > fread(temp, sizeof(temp), 1, data);
    > int first = temp >> 24;
    > int second = temp << 8 >> 24;
    > int third = temp << 16 >> 24;
    > int fourth = temp << 24 >> 24;
    >
    > I'm trying to do the same thing in Ruby, but I'm having difficulties with
    > the bitshifting. Any ideas? Would it make since to inline C code in Ruby
    > for this?
    >
    > Joe
    >
    >
     
    Lyndon Samson, Nov 18, 2004
    #4
  5. Joe Van Dyk

    Mark Hubbart Guest

    On Fri, 19 Nov 2004 08:05:21 +0900, Joel VanderWerf
    <> wrote:
    > Joe Van Dyk wrote:
    >
    >
    > > I have a bunch of binary data consisting of 4 ints stored inside an unsigned
    > > long.
    > >
    > > Here's psuedo C code to extract out the ints:
    > >
    > > // data is a FILE pointer to the file
    > > unsigned long temp;
    > > fread(temp, sizeof(temp), 1, data);
    > > int first = temp >> 24;
    > > int second = temp << 8 >> 24;
    > > int third = temp << 16 >> 24;
    > > int fourth = temp << 24 >> 24;
    > >
    > >
    > > I'm trying to do the same thing in Ruby, but I'm having difficulties with
    > > the bitshifting. Any ideas? Would it make since to inline C code in Ruby
    > > for this?

    >
    > irb(main):003:0> [1,2,3,4].pack "C*"
    > => "\001\002\003\004"
    > irb(main):004:0> [1,2,3,4].pack("C*").unpack("C*")
    > => [1, 2, 3, 4]


    Or, starting with the unsigned long;

    # pack('N') => network byte order;
    # pack('V') => little-endian byte order
    # pack('L') => native byte order
    [123456789].pack('N').unpack('C*')
    ==>[7, 91, 205, 21]

    ri unpack for more.

    hth,
    Mark
     
    Mark Hubbart, Nov 19, 2004
    #5
  6. Joe Van Dyk

    Joe Van Dyk Guest

    Joel VanderWerf wrote:
    > Joe Van Dyk wrote:
    >> I have a bunch of binary data consisting of 4 ints
    >> stored inside an unsigned long.
    >>
    >> Here's psuedo C code to extract out the ints:
    >>
    >> // data is a FILE pointer to the file
    >> unsigned long temp;
    >> fread(temp, sizeof(temp), 1, data);
    >> int first = temp >> 24;
    >> int second = temp << 8 >> 24;
    >> int third = temp << 16 >> 24;
    >> int fourth = temp << 24 >> 24;
    >>
    >>
    >> I'm trying to do the same thing in Ruby, but I'm having
    >> difficulties with the bitshifting. Any ideas? Would it
    >> make since to inline C code in Ruby for this?

    >
    > irb(main):003:0> [1,2,3,4].pack "C*"
    > => "\001\002\003\004"
    > irb(main):004:0> [1,2,3,4].pack("C*").unpack("C*")
    > => [1, 2, 3, 4]


    Ah.... I didn't know that I needed to have the "C*" part. Thanks.
     
    Joe Van Dyk, Nov 19, 2004
    #6
  7. Joe Van Dyk wrote:
    > Joel VanderWerf wrote:
    >>irb(main):003:0> [1,2,3,4].pack "C*"
    >>=> "\001\002\003\004"
    >>irb(main):004:0> [1,2,3,4].pack("C*").unpack("C*")
    >>=> [1, 2, 3, 4]

    >
    >
    > Ah.... I didn't know that I needed to have the "C*" part. Thanks.


    unpack("C4") will work too. You may want "c4" instead if you are using
    signed ints.
     
    Joel VanderWerf, Nov 19, 2004
    #7
    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. Replies:
    3
    Views:
    606
    Mark P
    Apr 3, 2005
  2. George Marsaglia

    Assigning unsigned long to unsigned long long

    George Marsaglia, Jul 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    749
    Eric Sosman
    Jul 8, 2003
  3. 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
  4. Daniel Rudy

    unsigned long long int to long double

    Daniel Rudy, Sep 19, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,247
    Peter Shaggy Haywood
    Sep 20, 2005
  5. Digital Puer

    extracting front bits from an unsigned long long?

    Digital Puer, Nov 11, 2005, in forum: C Programming
    Replies:
    36
    Views:
    1,381
    Mark McIntyre
    Nov 13, 2005
Loading...

Share This Page