写二进制字符串中的二进制文件的Python 3.4 [英] Write binary string in binary file Python 3.4

查看:166
本文介绍了写二进制字符串中的二进制文件的Python 3.4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在字符串中的二进制文件中写入二进制数据。我的字符串只包含0和1。我试图做这样

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)

但我只有 TXT 文件。没有人HEX主编正确地看到它。我在做什么错了? Python版本3.4
我有字符串,看起来像这样

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

下面是96 0和1,线路长度总是16的倍数,我需要这一套的0和1的二进制文件,但如果这样我在HEX readactor有写:

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

对于每1或0我坐8 symbold其ASCII code。

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

我试图让它在WHIS方式:

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

它可以正确处理 CHR(0b00010110)如果我写在文件中这个符号,在HEX主编,我将看到 00010110 正确的位,但这种方法不适用于我所有的字符串工作,价值观诠释192和22(MB多一些),我有一个错误:

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>

哪些错误?

我发现我的最后一个方法的问题。它与 UTF-8 编码,我尝试转换8位为char,在UTF-8,我们可以在1个字节只有7位的转换,因为1位(第一个)总是0。这样,我们就可以不带code数字比更01111111 。寻找下一个...

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...

推荐答案

我找到答案了用另一种编码,并写入字节这个问题:

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

我写的字符串以0和1:
    000101101100000000010010110000010011000000010010001000100000000000010111110000110100001100010001

I wrote string with 0 and 1: 000101101100000000010010110000010011000000010010001000100000000000010111110000110100001100010001

如果我打开文件,记事本,我会看到АБ0ГC有些符号没有显示...但如果​​我要在十六进制编辑者打开文件,我将看到:

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

最好的96位!

这篇关于写二进制字符串中的二进制文件的Python 3.4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆