Write binary string in binary file Python 3.4

2019-09-07 01:18发布

I tried to write binary data in string to binary file. My string contains only 0 and 1. I tried to do it in this way

file = open('file.bin','wb')
d = pack(str(len(code))+'s', bytes(code, 'UTF-8'))
file.write(d)

But i have only txt file. And no one HEX redactor see it correctly. What i am doing wrong? Python version 3.4 I have string which look like this

 000101101100000000010010110000010011000000010010001000100000000000010111110000110100001100010001

Here is 96 0 and 1, line length always a multiple of 16. I need this set of 0 and 1 in binary file, but if write in this way I have got in HEX readactor:

00110000 00110000 00110000 00110001 00110000 00110001 00110001 00110000 00110001 00110001 00110000 00110000 00110000 00110000 00110000 00110000
00110000 00110000 00110000 00110001 00110000 00110000 00110001 00110000 00110001 00110001 00110000 00110000 00110000 00110000 00110000 00110001
00110000 00110000 00110001 00110001 00110000 00110000 00110000 00110000 00110000 00110000 00110000 00110001 00110000 00110000 00110001 00110000
00110000 00110000 00110001 00110000 00110000 00110000 00110001 00110000 00110000 00110000 00110000 00110000 00110000 00110000 00110000 00110000
00110000 00110000 00110000 00110001 00110000 00110001 00110001 00110001 00110001 00110001 00110000 00110000 00110000 00110000 00110001 00110001
00110000 00110001 00110000 00110000 00110000 00110000 00110001 00110001 00110000 00110000 00110000 00110001 00110000 00110000 00110000

For every 1 or 0 i take 8 symbold its ASCII code.

I tried to make it in whis way:

cur = 0
while cur < len(code):
    file.write(chr(int(code[cur:cur+8], 2)))
    cur += 8

it works correctly for chr(0b00010110) if I write this symbol in file, in HEX redactor i will see 00010110 correct bits, but this method doesn't work for all my string, in values int 192 and 22 (mb some more) i have an error:

File "C:\Python34\lib\encodings\cp1251.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xc0' in position 0: character maps to <undefined>

Whats wrong?

I found a problem with my last method. It works with UTF-8 encoding, I try to convert 8bits to char, in UTF-8 we can convert only 7 bits in 1 byte, because 1 bit (first) always 0. In this way, we can't encode numbers more than 01111111. Looking for the next...

1条回答
迷人小祖宗
2楼-- · 2019-09-07 01:41

I found answer for this question in using another encoding, and writing bytes:

file = open('file.bin','wb')
cur = 0
while cur < len(code):
    c = int(code[cur:cur+8], 2)
    file.write(bytes(chr(c), 'iso8859-1'))
    cur += 8

I wrote string with 0 and 1: 000101101100000000010010110000010011000000010010001000100000000000010111110000110100001100010001

And if i open file with notepad I will see АБ0" ГC some symbols not shown... but if i will open file in hex redactor I will see:

00010110 11000000 00010010 11000001 00110000 00010010 00100010 00000000 00010111 11000011 01000011 00010001

Best 96 bits!

查看更多
登录 后发表回答