Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个整数。可以说,
var = 83 #less than 0xff
我有一个字节。所以假设我有字节 b 我想计算的整数值
b-=var #b as an integer value , possibly by eval(b)?
然后我想把它变成一个字节,我怎么能在python中做到这一点?
如果我理解了这个问题,你可以这样做:
>>> chr(ord('x') - 83) '%'
'x'你的字节在哪里。
'x'
如果您使用的是 Python 3.x
>>> bytes([ord(b'x') - 83]) b'%'
注意ord(b'x')与b'x'[0]
ord(b'x')
b'x'[0]
另一个示例(结果字节不可打印并显示在\x00表单中):
\x00
>>> chr(ord('\x53') - 83) '\x00'