python关于编码和hash

关于编码测试

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(),才能进行哈希运算

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值