最近在搞一個socket,用python向C#服務器發送bytes和從服務器接收bytes,搞了一天基本弄清楚了這些轉換關系。
建立一個空的bytes數組:
a=bytes(5) print(a)
執行結果:
b'\x00\x00\x00\x00\x00'
將int轉化為bytes(大端字節序):
def intToBytes(value, length):
result = []
for i in range(0, length):
result.append(value >> (i * 8) & 0xff)
result.reverse()
result_bytes=bytes(result)
return result_bytes
print(intToBytes(-95,3))
執行結果:
b'\xff\xff\xa1'下
下班了,后面補哈
將字符串轉為bytes:
