簡體   English   中英

按原樣將字符串轉換為字節 object

[英]Converting a string to a bytes object as is

如何將字符串按原樣轉換為字節 object,不對其進行編碼? 我不能在這里使用.encode() ,因為它在保存后破壞了我的二進制文件。

filedata = pathlib.Path('file.bin').read_bytes()
# since i can't modify a bytes object, i should convert it to a string, should I?
data = ''
for i in filedata:
    data += chr(i) if isinstance(i, int) else i
data[3] = '\x01'
data += '\x58\x02\x0C\x80\x61\x39\x56\x18\x55\x61\x89\x42\x42\x16\x46\x17\x54\x70\x10\x58\x60\x10\x10\x01\x75\x10\xF0\xC0\x00\x01\x00\x02\x00\xC0\x00\xD0\x00\x01\x00\xC4\x00\x01\x00\x02\x00\x01\x00\x00\x02\x00\x00\x00'
pathlib.Path('result.bin').write_bytes(data.encode()) # doesn't work as it should

所以不是這個:

58 02 0C 80 61 39 56 18 55 61 89 42 42 16 46 17 54 70 10 58 60 10 10 01 75 10 F0 C0 00 01 00 02 00 C0 00 D0 00 01 00 C4 00 01 00 02 00 01 00 00 02 00 00 00

我明白了:

58 02 0C C2 80 61 39 56 18 55 61 C2 89 42 42 16 46 17 54 70 10 58 60 10 10 01 75 10 C3 B0 C3 80 00 01 00 02 00 C3 80 00 C3 90 00 01 00 C3 84 00 01 00 02 00 01 00 00 02 00 00 00

我嘗試修改字節 object 本身,但我總是收到該錯誤:

TypeError: 'bytes' object 不支持項目分配

如何按原樣將字符串轉換為字節對象,即不對其進行編碼?

你不能。 這是術語的矛盾——從 Python 3 開始。

字符串是文本字符序列。 想想字母、標點符號、空格,甚至控制字符。 字節對象是一個 8 位數字序列。 這兩個序列如何相關是一個編碼問題。 沒有其他辦法了。

文本字符應被視為抽象實體。 例如,字母 A 就存在。 沒有與它相關聯的數字本身 (在內部,它由一個 Unicode 代碼點表示,它一個數字,但這是一個實現細節。)

在上面的代碼中,您正在讀取字節和寫入字節,在兩者之間您想要操作字節流:更改其中一個數字,附加其他數字。

Python bytes在這方面與str沒有什么不同:它們都是不可變類型。 如果您執行與上述相同但使用字符串的操作,則會出現相同類型的錯誤:

>>> s = 'abcd'
>>> s[3] = 'x'
TypeError: 'str' object does not support item assignment

也就是說,字符串不支持就地字符操作。 但是,還有其他方法可以實現相同的結果。 就地字節操作,而另一方面,支持-可以說,因為它是一個用例比字符串更為常見。 您只需要使用bytearray而不是bytes

>>> data = bytearray(b'\x00\x01\x02\x03\x04')
>>> data[3] = 255
>>> print(data)
bytearray(b'\x00\x01\x02\xff\x04')

然后您可以將其寫入文件而無需任何編碼:

pathlib.Path('result.bin').write_bytes(data)

(請注意, bytes文字必須以b為前綴。)

已解決(感謝 John):

filedata = bytearray(pathlib.Path(sys.argv[1]).read_bytes())
# filedata = bytearray(open(sys.argv[1], 'rb').read()) also works
filedata[1] = 255 # modifying a single byte (0 - 255)
filedata[0:1] = b'\xff' # inserting bytes
filedata.extend(255) # appending one single byte
filedata.extend(filedata2) # appending another array of bytes (bytearray object)
filedata.extend(b'\xff\xff') # appending bytes
filedata.extend([255, 255]) # appending bytes too
pathlib.Path(sys.argv[1]).write_bytes(filedata) # write data to a file
# open(sys.argv[1], 'rb').write(filedata) should work too

這最初是添加到問題的修訂版 5中的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM