每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法 by oflyhigh

View this thread on steempeak.com
· @oflyhigh ·
$30.84
每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法
还记得鲁迅小说《孔乙己》中提到的鲁迅小说《孔乙己》当中的茴香豆的“回字有四样写法”吗?孔乙己觉得自己掌握了茴香豆的几种写法,是一个有学识有文化的人。

![](https://cdn.pixabay.com/photo/2017/09/03/09/05/binary-code-2709626_960_720.jpg)
(图源 :[pixabay](https://pixabay.com/))

今天在写一段HMAC代码时,直接就用了
>`digest = hmac.new(key.encode('utf-8'), msg.encode('utf-8'), digestmod='sha256').hexdigest()`

然后突然想起来我以前习惯这样用:
>`hmac.new(bytes(key,'utf-8') , bytes(msg, 'utf-8'), digestmod='sha256').hexdigest()`

也就说说,把字符串(str)转成二进制数据/字节型数据(bytes),有两种方法,那么这两种方法有什么区别呢?

为了搞懂这个区别,我查了半天资料,然而实在觉得是没啥区别(至少在Python 3.6以后是这样),但是却被我又发现了两种转换方法。

我把它们整理一下,这样我就掌握了四种写法了,测试代码如下:

>`a = "苍茫的天涯是我的爱!"`
>`print(type(a))`
>
>`b = bytes(a, 'utf-8')`
>`print(b)`
>`print(type(b))`
>
>`c = a.encode('utf-8')`
>`print(c)`
>`print(type(c))`
>
>`d = str.encode(a, 'utf-8')`
>`print(d)`
>`print(type(d))`

>`import codecs`
>`e = codecs.encode(a, 'utf-8')`
>`print(e)`
>`print(type(e))`

输出如下:
>![image.png](https://images.hive.blog/DQmT1rha4dJwaMC5bQkePSsb8a5u4mw4P9Ns3hQ6z4uPsSr/image.png)

也就是说在python3中,无论是使用`bytes(a, 'utf-8')` 还是`a.encode('utf-8')` 还是 `str.encode(a, 'utf-8')` 或者`codecs.encode(a, 'utf-8')`都可以将字符串转换成bytes。

在测试完成后,我突然觉得我简直就是现代版的孔乙己啊,知道一种用法就行呗,非得学到四种没啥差别的用法,以后用到时,除了懵,还有啥好处呢?

当然也有可能我没掌握这几种用法真的差异和精髓,研究得不够透彻,不过我却不想研究下去了,有知道的朋友说一声吧。

当然了,顺便又重读了一遍《孔乙己》,说不出有啥感受,倒是有点伤感/(ㄒoㄒ)/~~

# 相关链接

* [孔乙己 (鲁迅小说)](https://baike.baidu.com/item/%E5%AD%94%E4%B9%99%E5%B7%B1/2262158)
* [每天进步一点点:HASH以及HMAC](https://hive.blog/cn/@oflyhigh/hash-hmac)
* [Python 3 - Encode/Decode vs Bytes/Str ](https://stackoverflow.com/questions/14472650/python-3-encode-decode-vs-bytes-str)
* [Best way to convert string to bytes in Python 3?](https://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 107 others
properties (23)
post_id85,986,426
authoroflyhigh
permlinkstr-bytes
categorycn
json_metadata{"tags":["cn","cutehive","cn-programming","bytes","string","encode"],"image":["https:\/\/cdn.pixabay.com\/photo\/2017\/09\/03\/09\/05\/binary-code-2709626_960_720.jpg","https:\/\/images.hive.blog\/DQmT1rha4dJwaMC5bQkePSsb8a5u4mw4P9Ns3hQ6z4uPsSr\/image.png"],"links":["https:\/\/pixabay.com\/","https:\/\/baike.baidu.com\/item\/%E5%AD%94%E4%B9%99%E5%B7%B1\/2262158","https:\/\/hive.blog\/cn\/@oflyhigh\/hash-hmac","https:\/\/stackoverflow.com\/questions\/14472650\/python-3-encode-decode-vs-bytes-str","https:\/\/stackoverflow.com\/questions\/7585435\/best-way-to-convert-string-to-bytes-in-python-3"],"app":"steemit\/0.2","format":"markdown"}
created2020-05-13 02:59:27
last_update2020-05-13 02:59:27
depth0
children1
net_rshares71,819,468,405,835
last_payout2020-05-20 02:59:27
cashout_time1969-12-31 23:59:59
total_payout_value16.164 SBD
curator_payout_value14.671 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,703
author_reputation1,148,153,621,496,884
root_title每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (171)
@resteemsupport ·
Hello oflyhigh!

Congratulations! This post has been randomly Resteemed! For a chance to get more of your content resteemed join the [Steem Engine Team](https://steemit.com/steemit/@steemengineteam/more-followers-more-votes-steemengine)
properties (22)
post_id85,986,441
authorresteemsupport
permlinkre-str-bytes-20200513t030034
categorycn
json_metadata{}
created2020-05-13 03:00:36
last_update2020-05-13 03:00:36
depth1
children0
net_rshares0
last_payout2020-05-20 03:00:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length236
author_reputation-5,666,740,605,819
root_title每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000