tags:

views:

71

answers:

2

How can I store and retrieve the number 2**10000 in a binary file in python without converting it to a string? Can it be stored as 10,000 bits?

+3  A: 

The built-in pickle module can do it if you pass in protocol version 2 ("new binary protocol"):

import pickle
pickle.dumps(2**10000, 2)

That returns a string of 1259 bytes. Of course, you'd want to write it to a file normally, so use pickle.dump(2**10000, file, 2)

Evgeny
+1  A: 

It's not clear to me if you are asking in general how to store large integers in a binary file or if the number 2**10000 is significant. If it is significant then using over a kilobyte to store it is obviously very wasteful (I can write it in 8 characters!)

I'll assume the general case, but for starters you'd need 10001 bits to store 2**10000, not 10000, so there's a question over what to do about the extra 7 bits needed to pad to a byte boundary in the file. I'm just going to store it in 10008 bits (1251 bytes). This solution uses the bitstring module.

from bitstring import Bits
fout = open('bignumber', 'wb')
a = Bits(uint=2**10000, length=10008)
a.tofile(fout)

and to read it back:

the_number = Bits(filename='bignumber').uint

This really does just store the number and nothing else in the file.

Scott Griffiths
In my case I do store exactly the same number of bits every time, so next time I look at the code I'll check if this is faster thanks.
Mark