python3 各类编码转换

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

 

 

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

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

输出:spa

<class 'str'> 414243

2.ascii码串转字符串:code

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

输出:blog

<class 'str'> 0A

 

 

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

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

输出:ci

b'\xa7\xb7'

4.字节串转16进制串字符串

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

输出:base64

a7b7

5.base64编码解码:class

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

输出:

b'hello'

输出是bytes,若是想要字符串就decode一下. 由于每每解完base64后还要其余的操做, 默认输出bytes就很方便了.

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

 

.

相关文章
相关标签/搜索