关于编码测试
a = ‘编码测试’
使用不同的编码格式给a进行编码
b = a.encode(‘utf-8’)
c = a.encode(‘gb2312’) # 发现gb2312和gbk结果一样
d = a.encode(‘gbk’)
print(type(b), b)
print(type©, c)
print(type(d), d)
‘’’
<class ‘bytes’> b’\xe7\xbc\x96\xe7\xa0\x81\xe6\xb5\x8b\xe8\xaf\x95’
<class ‘bytes’> b’\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4’
<class ‘bytes’> b’\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4’
‘’’
使用不同的解码方式解码
b1 = b.decode(‘utf-8’)
c1 = c.decode(‘gb2312’)
d1 = d.decode(“gbk”)
b11 = b.decode(‘gbk’) # b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
print(type(b1), b1)
print(type(c1), c1)
print(type(d1), d1)
print(type(b11), b11) # b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
“”"
<class ‘str’> 编码测试
<class ‘str’> 编码测试
<class ‘str’> 编码测试
<class ‘str’> 缂栫爜娴嬭瘯
“”"
a = ‘hello world’
使用不同的编码格式给a进行编码
b = a.encode(‘utf-8’)
c = a.encode(‘gb2312’) # 发现gb2312和gbk结果一样
d = a.encode(‘gbk’)
print(type(b), b)
print(type©, c)
print(type(d), d)
‘’’
<class ‘bytes’> b’\xe7\xbc\x96\xe7\xa0\x81\xe6\xb5\x8b\xe8\xaf\x95’
<class ‘bytes’> b’\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4’
<class ‘bytes’> b’\xb1\xe0\xc2\xeb\xb2\xe2\xca\xd4’
‘’’
使用不同的解码方式解码
b1 = b.decode(‘utf-8’)
c1 = c.decode(‘gb2312’)
d1 = d.decode(“gbk”)
b11 = b.decode(‘gbk’) # b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
print(type(b1), b1)
print(type(c1), c1)
print(type(d1), d1)
print(type(b11), b11) # b本来是用utf-8编码,现在用gbk进行解码,出现乱码的情况
“”"
<class ‘str’> 编码测试
<class ‘str’> 编码测试
<class ‘str’> 编码测试
<class ‘str’> 缂栫爜娴嬭瘯
“”"
关于python的hash
import hashlib
if __name__ == "__main__":
mess = str(input())
s = hashlib.sha256() # Get the hash algorithm.
s.update(mess.encode()) # Hash the data.
b = s.hexdigest() # Get he hash value.
print(b)
一定要对字符串进行
编码
,即eoconde()
,才能进行哈希运算