Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Extracting ints from a unsigned long (http://www.velocityreviews.com/forums/t818061-extracting-ints-from-a-unsigned-long.html)

Joe Van Dyk 11-18-2004 10:24 PM

Extracting ints from a unsigned long
 
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




Mauricio Fernández 11-18-2004 11:02 PM

Re: Extracting ints from a unsigned long
 
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/



Joel VanderWerf 11-18-2004 11:05 PM

Re: Extracting ints from a unsigned long
 
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]



Lyndon Samson 11-18-2004 11:36 PM

Re: Extracting ints from a unsigned long
 
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 <joe.vandyk@boeing.com> 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
>
>




Mark Hubbart 11-19-2004 04:10 AM

Re: Extracting ints from a unsigned long
 
On Fri, 19 Nov 2004 08:05:21 +0900, Joel VanderWerf
<vjoel@path.berkeley.edu> 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



Joe Van Dyk 11-19-2004 08:52 PM

Re: Extracting ints from a unsigned long
 
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.



Joel VanderWerf 11-19-2004 10:07 PM

Re: Extracting ints from a unsigned long
 
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.




All times are GMT. The time now is 07:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.