0

I have trouble with saving binary data to the file. For example, I am reading data from test.txt:

this is test of Huffman algorithm

When I 'compress' that I got binary:

101010111110001101111000110101010000001011101000111011000101100111110111001110011011011100011011000100110001001011111010101011100100

How to save it to the file like a binary? Because if I save like a string - file size equals to 132*8 cause that's UTF-8. All bytes methods work badly because they translate binary to bytes.

Mr.Xyzed
  • 43
  • 9
  • 3
    Binary to ASCII (and vice versa) has been discussed here: https://stackoverflow.com/questions/7396849/convert-binary-to-ascii-and-vice-versa – DocWeird Jun 12 '17 at 07:46
  • That method converts all 0 and 1 like string, so a size of result would be 1105 bytes... – Mr.Xyzed Jun 12 '17 at 07:59
  • What is that you're trying to accomplish, exactly? If you save a string in a file, it is in "binary" format, ie. just zero's and ones. Each character is represented by 8-bits, forming one byte (UTF-8 especially, even though ASCII is still "officially" 7-bits it's transmitted as 8-bits with it's high bit not set). If you want a human readable binary expression (which is also a valid Python expression) you can use bit(). – DocWeird Jun 12 '17 at 08:23

0 Answers0