python3 各种编码转换

在做CTF密码题时很大的坑点就在编码,中间有一个弄错就出不来结果。正好python在这块比较坑,记录一下。以下是各种需求对应的输出:

1. 字符串转16进制ascii码串:

txt='ABC'
new=txt.encode('utf-8').hex()
print(type(new), new)

输出:

<class 'str'> 414243

2.ascii码串转字符串:

code='3041'
new=bytes.fromhex(code).decode()
print(type(new), new)

输出:

<class 'str'> 0A

3.字符串形式的16进制,转字节串

str='A7B7'
c=bytes.fromhex( str )
print(c)

输出:

b'\xa7\xb7'

4.字节串转16进制串

code=b'\xa7\xb7'
new=code.hex()
print(new)

输出:

a7b7

5.base64编码解码:

from base64 import *
txt='aGVsbG8=' print(b64decode(txt))

输出:

b'hello'

输出是bytes,如果想要字符串就decode一下. 因为往往解完base64后还要其他的操作, 默认输出bytes就很方便了.

编码也类似,用b64encode, 此处省略.

.

猜你喜欢

转载自www.cnblogs.com/cnnnnnn/p/10850291.html
今日推荐