If I have the strings "0" and "1" how can I write them in a file in a way that each character only takes one bit?
2 Answers
You're not going to be able to write single bits to a file, as far as I know, but you may be able to write bytes using bytearray. Also, representing strings is different than representing integers, are you sure you want a string?

- 11,660
- 3
- 38
- 60
-
I have to implement Huffmman compresion code, and in order to compress the file I need that each character only takes a bit. – victor Oct 24 '15 at 16:37
-
You can only represent 2 characters using a bit, a bit's only possible values are 0 or 1. So you definitely can't represent very interesting characters as bits. – jgritty Oct 24 '15 at 16:38
-
that's what I want, know how to write characters "0" and "1" using only one bit in a text. – victor Oct 24 '15 at 16:40
-
@victor a side note: the above mentioned `bytearray` module is capable of en- and de-coding huffman codes. – hiro protagonist Oct 29 '15 at 13:07
You can convert bits to 8-bit strings (bits are padded with zeros to form the full bytes if necessary):
>>> bits = "1000001"
>>> int2bytes(int(bits, 2))
'A'
If the number of bits is divisible by 8 then each "0"
, "1"
in the input is converted to exactly one bit in the result. Otherwise, each input bit requires ((n + 7) // 8) * 8 / (1.*n)
bits per bit (only slightly more than 1 for large n
) e.g., if the number of bits n=1001
then the ratio is ~1.007
.
Leading zeros are forgotten. You can preserve them if you always prepend "1"
to bits while saving and removing it while loading the bits back.
To save bits to a file:
with open(filename, 'wb') as file: # open in binary mode
file.write(int2bytes(int(bits, 2)))
where int2bytes()
:
import binascii
def int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
To do the reverse, see Convert binary to ASCII and vice versa