Extracting ints from a unsigned long

J

Joe Van Dyk

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
 
M

Mauricio Fernández

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")
 
J

Joel VanderWerf

Joe said:
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]
 
L

Lyndon Samson

Surely they must be 4 shorts ( 16bits ) to be stored inside one long ( 64bits )?
 
M

Mark Hubbart

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
 
J

Joe Van Dyk

Joel said:
Joe said:
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.
 
J

Joel VanderWerf

Joe said:
Joel said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top