Large integers from a file

B

BT

Hi,

I am new to Ruby and have been unable to figure out how to accomplish
the following task:

I would like to read a certain number of bytes from a binary file, then
convert those bytes into a large integer.

Say the 1st 4 bytes of the binary file were 0xAF 0xBB 0x1C 0xFF. I'd
like to read those 4 bytes from the file and convert them into the
[base 10] integer, 2,948,275,455.

That was a simple example; I would be reading much larger groups of
bytes at a time.

Any help/pointers would be greatly appreciated!

BT
 
C

ChrisH

Hi, just played with this, so may need work 9^)

Try

File.open('abinaryfile','rb'){|bf|
buff = bf.read(4) #read 4 bytes into a string buffer
value = buff.unpack('N') #unpack the 4 bytes as an unsigned long in
network byte order
#...do something with value
}


Cheers
Chris
 
B

BT

Hey,

Thanks for the reply Chris!

I've looked at the docs for 'unpack' and there doesn't seem to be a
specifier that I can use to convert to a Bignum. :(

Again, I will be reading large numbers of bytes at a time...

I guess the question becomes, how can I convert an array of bytes to an
instance of Bignum?

Thanks,
Reed
 
D

Dave Burt

BT said:
Hi,

I am new to Ruby and have been unable to figure out how to accomplish
the following task:

I would like to read a certain number of bytes from a binary file, then
convert those bytes into a large integer.

Say the 1st 4 bytes of the binary file were 0xAF 0xBB 0x1C 0xFF. I'd
like to read those 4 bytes from the file and convert them into the
[base 10] integer, 2,948,275,455.

n = array_of_bytes.inject(0){|n, b| (n << 8) + b }

With a file, you might try something like this:

n = 0
File.open "filename" do |f|
4.times { n = n << 8 + f.getc }
end

Cheers,
Dave
 
R

Robert Klemme

Dave said:
BT said:
Hi,

I am new to Ruby and have been unable to figure out how to accomplish
the following task:

I would like to read a certain number of bytes from a binary file, then
convert those bytes into a large integer.

Say the 1st 4 bytes of the binary file were 0xAF 0xBB 0x1C 0xFF. I'd
like to read those 4 bytes from the file and convert them into the
[base 10] integer, 2,948,275,455.

n = array_of_bytes.inject(0){|n, b| (n << 8) + b }

With a file, you might try something like this:

n = 0
File.open "filename" do |f|
4.times { n = n << 8 + f.getc }
end

For efficiency reasons both approaches can be combined:

File.open "filename" do |f|
n = 0
4.times { n = (n << 32) | (f.read(4).unpack("N")[0]) }
n
end

This one is even nicer:

File.open("your-file", "rb") do |f|
f.read(6 * 4).unpack("N6").inject(0) {|sum, x| sum << 32 | x}
end

Kind regards

robert
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top