0

I created a compressor for my online course project using huffman algorithm. Problem is that the output file in binary (10101010011..) is bigger than the original files.

Teachers on the course don't know the answer.

I use this, where ciph_text is a string of 0s and 1s.

with open(full_name,'w') as temp:
    temp.write(ciph_text)

Any idea?
I can post more code if requested.

martineau
  • 119,623
  • 25
  • 170
  • 301
D4N
  • 11
  • 1
  • 7
    because you are saving as a string, so every character counts as 8 bits instead of 1 – BlackBear Jan 08 '19 at 10:11
  • 1
    also avoid `with open(full_name,'w')` for binary, use `wb` – Jean-François Fabre Jan 08 '19 at 10:13
  • 3
    Don't know about huffmann algorithm, but anyway: Do you save a binary representation of a string, which is longer than the original string as ascii data in a file? I think you should save your bits in some bytes as a real binary file by `with open(full_name,'wb') as temp:` – SpghttCd Jan 08 '19 at 10:13
  • Yes, you do need to added more code to your question—otherwise all we can do is guess. – martineau Jan 08 '19 at 10:15
  • @SpghttCd: It's used to do [Huffman coding](https://en.wikipedia.org/wiki/Huffman_coding). – martineau Jan 08 '19 at 10:18
  • 2
    D4N: Suggest you take look at this [answer](https://stackoverflow.com/a/10691412/355230) of mine which describes a way to do bitwise I/O—because I think you're writing the resulting "bits" as a text file composed of `0` and `1` _characters_ which each are at least 8-bits long, not as binary zeros and ones. – martineau Jan 08 '19 at 10:25
  • I have a string of 1s and 0s and I want to save it as a binary file. Is there any easier way? – D4N Jan 24 '19 at 11:25

1 Answers1

1

int(x, 2) is your friend:

>>> a="00001010000101000001111000101000"
>>> for b in range(0, len(a), 8):
...   print a[b:b+8], int(a[b:b+8], 2)
... 
00001010 10
00010100 20
00011110 30
00101000 40
dede
  • 706
  • 9
  • 19