简体   繁体   English

在python中将ASCII字符串写为二进制

[英]Writing an ASCII string as binary in python

I have a ASCII string = "abcdefghijk". 我有一个ASCII字符串=“abcdefghijk”。 I want to write this to a binary file in binary format using python. 我想用python将它写成二进制格式的二进制文件。

I tried following: 我试过以下:

str  = "abcdefghijk"
fp = file("test.bin", "wb")
hexStr = "".join( (("\\x%s") % (x.encode("hex"))) for x in str)
fp.write(hexStr)
fp.close()

However, when I open the test.bin I see the following in ascii format instead of binary. 但是,当我打开test.bin时,我看到以ascii格式而不是二进制格式。

\x61\x62\x63\x64\x65\x66\x67

I understand it because for two slashes here ("\\\\x%s"). 我理解它,因为这里有两个斜杠(“\\\\ x%s”)。 How could I resolve this issue? 我该如何解决这个问题? Thanks in advance. 提前致谢。

Update : 更新:

Following gives me the expected result: 以下给出了预期的结果:

file = open("test.bin", "wb")
file.write("\x61\x62\x63\x64\x65\x66\x67")
file.close() 

But how do I achieve this with "abcdef" ASCII string. 但是如何使用“abcdef”ASCII字符串实现此目的。 ?

You misunderstood what \\xhh does in Python strings. 你误解了Python字符串中的\\xhh做了什么。 Using \\x notation in Python strings is just syntax to produce certain codepoints. 在Python字符串中使用\\x表示法只是生成某些代码点的语法

You can use '\\x61' to produce a string, or you can use 'a' ; 您可以使用'\\x61'生成字符串,也可以使用'a' ; both are just two ways of saying give me a string with a character with hexadecimal value 61, eg the a ASCII character : 都是说给我一个字符串与十六进制值61,例如一个字符的只是两种方式a ASCII字符

>>> '\x61'
'a'
>>> 'a'
'a'
>>> 'a' == '\x61'
True

The \\xhh syntax then, is not the value ; 然后, \\xhh语法不是值 ; there is no \\ and no x and no 6 and 1 character in the final result. 在最终结果中没有\\且没有x ,也没有61字符。

You should just write your string : 你应该写你的字符串

somestring = 'abcd'

with open("test.bin", "wb") as file:
    file.write(somestring)

There is nothing magical about binary files; 二进制文件没什么神奇之处; the only difference with a file opened in text mode is that a binary file will not automatically translate \\n newlines to the line separator standard for your platform; 在文本模式下打开的文件,唯一的区别是,一个二进制文件将不会自动转化\\n换行符为平台的行分隔符标准; eg on Windows writing \\n produces \\r\\n instead. 例如,在Windows上写\\n生成\\r\\n而不是。

You certainly do not have to produce hexadecimal escapes to write binary data. 您当然不必生成十六进制转义来写二进制数据。

On Python 3 strings are Unicode data and cannot just be written to a file without encoding, but on Python the str type is already encoded bytes. 在Python 3上,字符串是Unicode数据,不能在没有编码的情况下写入文件,但在Python上, str类型已经是编码字节。 So on Python 3 you'd use: 所以在Python 3上你会使用:

somestring = 'abcd'

with open("test.bin", "wb") as file:
    file.write(somestring.encode('ascii'))

or you'd use a byte string literal; 或者你使用字节串文字; b'abcd' . b'abcd'

I think you don't necessarily understand what binary/ascii is ... all files are binary in the sense that its just bits. 我认为你不一定了解二进制文件/ ascii是什么...所有文件都是二进制的,只是它的位数。 ascii is just a representation of some bits... 99.9999 % of file editors will display your bits as ascii if they can , and if there is no other encoding declared in the file itself ... ascii只是一些位的表示...如果可以的话,99.9999%的文件编辑器会将您的位显示为ascii,如果文件本身没有声明其他编码...

fp.write("abcd") 

is exactly equivelent to 完全相同的

fp.write("\x61\x62\x63\x64")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM