Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Extracting ints from a unsigned long

Reply
Thread Tools

Extracting ints from a unsigned long

 
 
Joe Van Dyk
Guest
Posts: n/a
 
      11-18-2004
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



 
Reply With Quote
 
 
 
 
Mauricio Fernández
Guest
Posts: n/a
 
      11-18-2004
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/


 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      11-18-2004
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]


 
Reply With Quote
 
Lyndon Samson
Guest
Posts: n/a
 
      11-18-2004
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 <(E-Mail Removed)> 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
>
>



 
Reply With Quote
 
Mark Hubbart
Guest
Posts: n/a
 
      11-19-2004
On Fri, 19 Nov 2004 08:05:21 +0900, Joel VanderWerf
<(E-Mail Removed)> 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


 
Reply With Quote
 
Joe Van Dyk
Guest
Posts: n/a
 
      11-19-2004
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.


 
Reply With Quote
 
Joel VanderWerf
Guest
Posts: n/a
 
      11-19-2004
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting front bits from an unsigned long long? Digital Puer C Programming 36 11-13-2005 12:05 PM
unsigned long long int to long double Daniel Rudy C Programming 5 09-20-2005 02:37 AM
Iterator Question for map of ints to set of ints uclamathguy@gmail.com C++ 3 04-03-2005 03:26 AM
ints ints ints and ints Skybuck Flying C Programming 24 07-10-2004 04:48 AM
Assigning unsigned long to unsigned long long George Marsaglia C Programming 1 07-08-2003 05:16 PM



Advertisments