1

I'm implementing Huffman Algorithm, but when I got the final compressed code, I've got a string similar to below:

10001111010010101010101

This is a binary code to created by the paths of my tree's leafs.

I have this sequence, but when I save it in a file, all that happens is system saving it as a ASCII on a file, which I can't compress because it has the same or bigger size than the original.

How do I save this binary directly?

PS: if I use some function to convert my string to binary, all I got is my ASCII converted to binary, so I did nothing, I need a real solution.

2
  • 2
    And how many bytes is this supposed to be written as? Big endian, little endian? Commented Aug 8, 2014 at 22:05
  • You could use the Bitwise I/O recipe mentioned in this answer of mine to another question about reading bits. Commented Aug 9, 2014 at 3:19

2 Answers 2

1

What you need to do is take each 8 bits and convert it into a byte to write out, looping until you have less than 8 bits remaining. Then save whatever's left over to prepend in front of the next value.

def binarize(bitstring):
    wholebytes = len(bitstring) // 8
    chars = [chr(int(bitstring[i*8:i*8+8], 2)) for i in range(wholebytes)]
    remainder = bitstring[wholebytes*8:]
    return ''.join(chars), remainder
Sign up to request clarification or add additional context in comments.

2 Comments

How to get back this value from the file, because your solution have worked, but I have no idea how to read it back.
@user3923894 that sounds like another question.
0

I think you just want int() with a base value of 2:

my_string = "10001111010010101010101"
code_num = int( my_string, 2 )

Then write to a binary file. struct.pack additionally allows you to specify whatever byte order you like.

myfile = open("filename.txt",'wb')
mybytes = struct.pack( 'i', code_num )
myfile.write(mybytes)
myfile.close()

This method will also write some number of leading zeros, which could cause trouble for your Huffman codes.

2 Comments

Huffman codes tend to be a variable number of bits, so you can't convert to a constant size integer.
Edited to allow larger ints

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.