目录
str和bytes
>>> Str = "PK\x03\x04"
>>> Str.encode()
b'PK\x03\x04'
>>> Bytes = b'PK\x03\x04'
>>> Bytes.decode()
"PK\x03\x04"
>>> Str = "504B0304"
>>> bytes.fromhex(Str)
b'PK\x03\x04'
>>> Bytes = b'PK\x03\x04'
>>> Bytes.hex()
"504b0304"
举例:以hex形式,读取文件和写入文件
with open('in', 'rb') as f1:
Str = f1.read().hex()
# Str = "504B0304"
Str1 = ""
for i in range(0, len(Str), 2):
Str1 += Str[i : i+2]
with open('out', 'wb') as f2:
f2.write(bytes.fromhex(Str1))
int和bytes
>>> from Crypto.Util.number import *
>>> Bytes = b'flag{}'
>>> bytes_to_long(Bytes)
112615676672893
>>> Int = 112615676672893
>>> long_to_bytes(Int)
b'flag{}'
>>> import libnum
>>> Bytes = b'flag{}'
>>> libnum.s2n(Bytes)
112615676672893
>>> Int = 112615676672893
>>> libnum.n2s(Int)
b'flag{}'
进制转换
>>> 0b1010
10
>>> 0o12
10
>>> 0xa
10
>>> int('0b1010', 2)
10
>>> int('0o12', 8)
10
>>> int('0xa', 16)
10
>>> int('1010', 2)
10
>>> int('12', 8)
10
>>> int('a', 16)
10
>>> bin(10)
'0b1010'
>>> oct(10)
'0o12'
>>> hex(10)
'0xa'
>>> bin(10)[2:]
'1010'
>>> oct(10)[2:]
'12'
>>> hex(10)[2:]
'a'