为什么第一个字节打印为(b'c\x…)而不是(b'x63\x…)?

2024-10-03 02:47:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个填充oracle攻击的程序,需要bytearrays, 但是如果我定义一个新的bytearray,那么第一个字节0x63的打印方式就不同了

我必须按字节XOR2bytearrays

test = bytearray( [99,203,00] )
print(test)
print(hex(99))

输出:

bytearray(b'c\xcb\x00')
0x63

这是我的第一个问题。谢谢你的帮助

f


Tags: test程序字节定义方式oracleprinthex
1条回答
网友
1楼 · 发布于 2024-10-03 02:47:51

对于字符串输出,python将可打印的十六进制代码替换为chr(hexcode)字符,以便显示:

print('c', ord('c'),hex(ord('c')))   #  c 99 '0x63'

t = bytearray([99,203,0])
print(t)                             #  bytearray(b'c\xcb\x00')
print(t[0],t[1],t[2])                #  99 203 0

它们是等价的,但打印出来的时间更短。您可以得到一个全十六进制表示,如下所示:

t = bytearray([99,203,0])
t_hex = '\\'+'\\'.join( ( hex(i) for i in t) ) 

print(t_hex)

输出:

\0x63\0xcb\0x0 

相关问题 更多 >