0

I have a big number that is basically a wall of binary in base 2. How can I write those bits to a file as raw bytes that I can read and make out what the number was without storing the actual number? Also, how do I decode the said raw bytes once they are in the file?

binary: 0b101010011000010000 decimal: 173584 byte: ???

user3023610
  • 51
  • 1
  • 5

2 Answers2

0

If the random, arbitrarily long sequence of bits represent a non-negative base 2 number, groups of 8 of those bits can be consider digits of a base-256 number and they can be written to a file as a string of 8-bit characters. Reversing the process after reading the string back in allows this string to be decoded back into the original number.

The following is something I derived from the this answer to the question Base 62 conversion in Python.

def base256_encode(n):  # non-negative integer to byte string
    if n > 0:
        arr = []
        while n:
            n, rem = divmod(n, 256)
            arr.append(chr(rem))
        return ''.join(reversed(arr))
    elif n == 0:
        return '\x00'
    else:
        raise ValueError("can't convert negative value to a byte string")

def base256_decode(s):  # (big-endian) byte string to number
    return reduce(lambda a, c: a*256 + ord(c), s, 0)

def binrepr(s):  # utility
    return ' '.join(('{:08b}'.format(ord(c))) for c in s)

n = 173584
print 'n:', n

en = base256_encode(n)
print 'binary bytes:', binrepr(en)

with open('rawbits.bin', 'wb') as outf:
    outf.write(en)

with open('rawbits.bin', 'rb') as inf:
    print 'read from file:', base256_decode(inf.read())

Output:

n: 173584
binary bytes: 00000010 10100110 00010000
read from file: 173584
Community
  • 1
  • 1
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Looks like an inefficient way to reimplement `int`->`bytes` conversion that Python already have. [Converting int to bytes in Python 3 - Stack Overflow](https://stackoverflow.com/questions/21017698/converting-int-to-bytes-in-python-3) – user202729 Feb 08 '21 at 14:23
  • @user202729: FWIW the code was written to work in Python 2—something that's fairly obvious from the use of `print` statements (plus the fact that the question wasn't tagged "python-3.x"). – martineau Feb 08 '21 at 15:09
0

I'm not sure if this helps or if it is answering your question directly but ...

If you want to exclusively access the file in python you could just pickle the thing. Something like this:

>>> x = 0b0010101011010100101101010010101100001011101010001010101001010110110101011011111111010100100111110101001000100111011101101
>>> x
444781385027604204959187557780049645
>>> import pickle
>>> pickle.dump(x, open('trash.txt', 'wb'))
>>> pickle.load(open('trash.txt', 'rb'))
444781385027604204959187557780049645

The big number will be stored in the file 'trash.txt' which can be read at your leisure but I don't think it will be useful outside of the realm of python.

shrewmouse
  • 5,338
  • 3
  • 38
  • 43