python 下各种字节的转化

由于在写CTF或者是写网络编程的时候,经常遇见二进制转化的问题,记录一下

[0xab, 0xbc, 0xcd, 0xde] -> ‘\xab\xbc\xcd\xde’

1
2
s = [0xab, 0xbc, 0xcd, 0xef]
print(bytes(s))

‘\xde\xad\xbe\xef’ -> [222, 173, 190, 239]

1
2
3
s = '\xde\xad\xbe\xef'
res = list(s)
# res [222, 173, 190, 239]

‘\xab\xbc\xcd\xde’ -> ‘abbccdde’

1
2
3
s = '\xab\xbc\xcd\xde'
res = binascii.hexlify(s)
# res = abbccdde

‘aabbccdd’ -> ‘\xaa\xbb\xcc\xdd’

1
2
3
s = 'aabbccdd'
res = binasicc.unhexlify(s)
# res = \xaa\xbb\xcc\xdd

‘\xde\xad\xbe\xef’ -> 0xdeadbeef

1
2
3
s = '\xde\xad\xbe\xef' 
res = int.from_bytes(s, 'big')
# res = 0xdeadbeef

0xdeadbeef -> ‘\xde\xad\xbe\xef’

1
2
s = 0xdeadbeef
print(int.to_bytes(s, 4, 'big'))

目前我们建立了

0xdeadbeef -> ‘\xde\xad\xbe\xef’ -> ‘deadbeef’ -> [222, 173, 190, 239]