[英]Python concatenate bytes to str
是否可以將字節連接到 str?
>>> b = b'this is bytes'
>>> s = 'this is string'
>>> b + s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't concat str to bytes
>>>
基於上面的簡單代碼,這是不可能的。
我問這個的原因是因為我看到了一個字節已連接到 str 的代碼? 這是代碼的片段。
buf = ""
buf += "\xdb\xd1\xd9\x74\x24\xf4\x5a\x2b\xc9\xbd\x0e\x55\xbd"
buffer = "TRUN /.:/" + "A" * 2003 + "\xcd\x73\xa3\x77" + "\x90" * 16 + buf + "C" * (5060 - 2003 - 4 - 16 - len(buf))
您可以在此處查看完整代碼。
http://sh3llc0d3r.com/vulnserver-trun-command-buffer-overflow-exploit/
將字符串編碼為字節以獲得以字節為單位的結果:
print(b'byte' + 'string'.encode())
# b'bytestring'
或者將字節解碼為字符串以獲得作為 str 的結果:
print(b'byte'.decode() + 'string')
# bytestring
第二個代碼片段顯示了連接的字符串。 您需要將字節轉換為字符串(如問題Convert bytes to a string 中所示)。 試試這個: b.decode("utf-8") + s
。 它應該給你你需要的輸出。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.