7

Suppose I have a number like 824 and I write it to a text file using python. In the text file, it will take 3 bytes space. However, If i represent it using bits, it has the following representation 0000001100111000 which is 2 bytes (16 bits). I was wondering how can I write bits to file in python, not bytes. If I can do that, the size of the file will be 2 bytes, not 3. Please provide code. I am using python 2.6. Also, I do not want to use any external modules that do not come with the basic installation I tried below and gave me 12 bytes!

a =824;
c=bin(a)
handle = open('try1.txt','wb')
handle.write(c)
handle.close()

3 Answers 3

10

The struct module is what you want. From your example, 824 = 0000001100111000 binary or 0338 hexadecimal. This is the two bytes 03H and 38H. struct.pack will convert 824 to a string of these two bytes, but you also have to decide little-endian (write the 38H first) or big-endian (write the 03H first).

Example

>>> import struct
>>> struct.pack('>H',824) # big-endian
'\x038'
>>> struct.pack('<H',824) # little-endian
'8\x03'
>>> struct.pack('H',824)  # Use system default
'8\x03'

struct returns a two-byte string. the '\x##' notation means (a byte with hexadecimal value ##). the '8' is an ASCII '8' (value 38H). Python byte strings use ASCII for printable characters, and \x## notation for unprintable characters.

Below is an example writing and reading binary data to a file. You should always specify the endian-ness when writing to and reading from a binary file, in case it is read on a system with a different endian default:

import struct

a = 824
bin_data = struct.pack('<H',824)
print 'bin_data length:',len(bin_data)

with open('data.bin','wb') as f:
    f.write(bin_data)

with open('data.bin','rb') as f:
   bin_data = f.read()
   print 'Value from file:',struct.unpack('<H',bin_data)[0]

print 'bin_data representation:',repr(bin_data)
for i,c in enumerate(bin_data):
    print 'Byte {0} as binary: {1:08b}'.format(i,ord(c))

Output

bin_data length: 2
Value from file: 824
bin_data representation: '8\x03'
Byte 0 as binary: 00111000
Byte 1 as binary: 00000011
Sign up to request clarification or add additional context in comments.

2 Comments

But, for say 5, I can represent it using 1 byte. I dont want 2 bytes in that case. What is the solution to this? I want to use the minimum number of bytes
Read the struct documentation. Use a B instead of H if the value fits in a byte.
5

Have a look at struct:

>>> struct.pack("h", 824)
'8\x03'

2 Comments

can you please elaborate on the above. Say with some code on how to convert 824 to binary format and write to a file the 'bitwise' way
That is, in the file it should occupy 2 bytes, not 3
2

I think what you want is to open the file in binary mode:

open("file.bla", "wb")

However, this will write an integer to the file, which will probably be 4 bytes in size. I do not know if Python has a 2 byte integer type. But you can circumvent that by encoding 2 16 bit number in one 32 bit number:

a = 824
b = 1234
c = (a << 16) + b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.