代码改变世界

易用常用的小知识点

2019-08-08 17:19  在上海的日子里  阅读(92)  评论(0编辑  收藏  举报

 

一、关于字符串转为二进制数据的方法

方法:

b'hello'     与   ‘hello’.encode('utf-8')   作用相同

import hashlib

md = hashlib.md5()  # 生成一个造密文的对象
val = '啦啦啦啦'   # 加盐处理
md.update(val.encode('utf-8'))

# md.update('hello'.encode('utf-8'))  # 将字符串转为二进制形式
md.update(b'hello')  # Bytes类型也就是二进制形式,  b'hello'  与 'hello'.encode('utf-8') 作用是一样的

res = md.hexdigest()
print(res)