0

i have a string of bytes like temp="101110110011" i want to write this string in binary file. i did it by :

with open("some binary file.bin", "wb") as f:
    bytes = str.encode (some string)
    f.write(bytes)

but because I use it for huffman encoding it will make my bin file size larger

how to save above string as binary directly in python 3 my string range is more than 32 bits

9
  • 1
    What problem are you seeking help with exactly? Commented Nov 20, 2016 at 12:12
  • 1
    Do you expect "10111011" to occupy 1 byte or 8 in the file? Commented Nov 20, 2016 at 12:12
  • "101110110011" looks suspiciously like a string of bits, not bytes. Are missing a conversion step? Commented Nov 20, 2016 at 12:17
  • yes I have string of bits. now how I write this string in binary file. Commented Nov 20, 2016 at 14:07
  • from array import * bin_array = array('B') bin_array.append(int('011',2)) bin_array.append(int('010',2)) bin_array.append(int('110',2)) f = file('binary.mydata','wb') bin_array.tofile(f) f.close() Commented Nov 20, 2016 at 14:15

0

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.