还记得鲁迅小说《孔乙己》中提到的鲁迅小说《孔乙己》当中的茴香豆的“回字有四样写法”吗?孔乙己觉得自己掌握了茴香豆的几种写法,是一个有学识有文化的人。 ![](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)
post_id | 85,986,426 |
---|---|
author | oflyhigh |
permlink | str-bytes |
category | cn |
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"} |
created | 2020-05-13 02:59:27 |
last_update | 2020-05-13 02:59:27 |
depth | 0 |
children | 1 |
net_rshares | 71,819,468,405,835 |
last_payout | 2020-05-20 02:59:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 16.164 SBD |
curator_payout_value | 14.671 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 1,703 |
author_reputation | 1,148,153,621,496,884 |
root_title | 每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
adm | 0 | 29,695,055,631,339 | 100% | ||
thecryptodrive | 0 | 603,999,144,463 | 25% | ||
kosimoos | 0 | 551,866,636 | 50% | ||
gidlark | 0 | 5,534,636,556 | 10% | ||
nascimentoab | 0 | 503,108,047 | 3% | ||
sharker | 0 | 2,366,889,091 | 11% | ||
rea | 0 | 1,494,895,493,602 | 40% | ||
yogi | 0 | 13,017,714,806 | 25% | ||
joythewanderer | 0 | 52,140,323,734 | 50% | ||
ace108 | 0 | 260,374,663,138 | 25% | ||
kibela | 0 | 3,091,823,726 | 11% | ||
laoyao | 0 | 40,749,928,142 | 100% | ||
somebody | 0 | 10,069,595,157 | 100% | ||
midnightoil | 0 | 143,125,749,916 | 100% | ||
xiaohui | 0 | 806,453,350,704 | 100% | ||
oflyhigh | 0 | 1,285,887,818,901 | 100% | ||
yulan | 0 | 14,692,278,074 | 100% | ||
rivalhw | 0 | 423,551,666,314 | 100% | ||
nextgen622 | 0 | 668,946,467,681 | 100% | ||
helene | 0 | 455,971,862,495 | 100% | ||
ffcrossculture | 0 | 476,523,112,195 | 100% | ||
sweetsssj | 0 | 18,463,155,953,047 | 50% | ||
netaterra | 0 | 213,212,802,450 | 16% | ||
penguinpablo | 0 | 646,364,031,435 | 16% | ||
funnyman | 0 | 234,811,175,586 | 25% | ||
michelle.gent | 0 | 19,494,868,444 | 100% | ||
lexikon082 | 0 | 1,221,201,398 | 100% | ||
belladonna | 0 | 1,875,159,409 | 100% | ||
dapeng | 0 | 35,045,444,850 | 100% | ||
justinashby | 0 | 18,565,630,536 | 100% | ||
passion-fruit | 0 | 5,754,668,665 | 100% | ||
htliao | 0 | 23,309,019,901 | 20% | ||
fortune-master | 0 | 5,522,656,319 | 100% | ||
curly-q | 0 | 2,251,668,397 | 100% | ||
eval | 0 | 733,497,131 | 100% | ||
maxer27 | 0 | 26,682,866,220 | 20% | ||
blacklux | 0 | 15,388,206,921 | 25% | ||
ew-and-patterns | 0 | 119,067,390,813 | 16% | ||
techken | 0 | 412,880,158 | 1.6% | ||
susanli3769 | 0 | 136,690,195,000 | 100% | ||
otom | 0 | 7,763,718,797 | 20% | ||
travelgirl | 0 | 91,418,963,260 | 40% | ||
catwomanteresa | 0 | 60,404,130,430 | 50% | ||
helgapn | 0 | 6,227,319,301 | 100% | ||
oldman28 | 0 | 22,544,262,068 | 48% | ||
liangfengyouren | 0 | 3,250,944,907 | 50% | ||
cn-reader | 0 | 5,214,614,737 | 50% | ||
tvb | 0 | 19,944,690,977 | 30% | ||
skenan | 0 | 8,492,225,131 | 50% | ||
floatinglin | 0 | 3,684,856,850 | 100% | ||
rocky1 | 0 | 4,009,948,237,140 | 4% | ||
kimzwarch | 0 | 8,136,712,951 | 4% | ||
bboyady | 0 | 920,964,534 | 5% | ||
saltiberra | 0 | 1,128,853,254 | 10% | ||
accelerator | 0 | 53,370,927,917 | 7.16% | ||
chinchilla | 0 | 242,963,873,227 | 100% | ||
davidke20 | 0 | 12,823,468,125 | 10% | ||
rosatravels | 0 | 29,036,644,012 | 5% | ||
robertoueti | 0 | 1,912,117,331 | 3% | ||
xiaoshancun | 0 | 29,889,671,604 | 100% | ||
rollsman | 0 | 5,152,634,446 | 100% | ||
tatjanastan | 0 | 407,660,848,920 | 41% | ||
pataty69 | 0 | 27,616,923,230 | 20% | ||
windowglass | 0 | 206,041,268,003 | 30% | ||
etherpunk | 0 | 1,419,877,823 | 25% | ||
itchyfeetdonica | 0 | 116,474,350,164 | 50% | ||
cryptonized | 0 | 76,811,693,695 | 16% | ||
jesusjacr | 0 | 74,391,670,185 | 20% | ||
yikloongye | 0 | 0 | 10% | ||
johnhooi | 0 | 10,347,323 | 10% | ||
maiyude | 0 | 130,192,608,401 | 50.55% | ||
suhunter | 0 | 954,664,315 | 50% | ||
yumisee | 0 | 84,931,665 | 10% | ||
casberp | 0 | 4,910,601,596 | 3% | ||
joeliew | 0 | 309,394,605 | 10% | ||
hkit98 | 0 | 0 | 10% | ||
elvinmas001 | 0 | 0 | 10% | ||
watersoo | 0 | 285,728,675 | 10% | ||
vamos-amigo | 0 | 264,634,363 | 10% | ||
sudefteri | 0 | 14,166,007,129 | 100% | ||
happy-soul | 0 | 32,967,723,195 | 10% | ||
iipoh06 | 0 | 8,072,494 | 10% | ||
annabellenoelle | 0 | 3,154,395 | 10% | ||
yethui | 0 | 23,151,740 | 10% | ||
riczlook | 0 | 12,324,019 | 10% | ||
andrewnoel | 0 | 0 | 10% | ||
fishbb | 0 | 1,440,608,020 | 25.27% | ||
fredo77200 | 0 | 3,665,154,696 | 100% | ||
onepercentbetter | 0 | 1,164,889,141 | 0.5% | ||
mariuszkarowski | 0 | 14,951,651,251 | 100% | ||
therising | 0 | 4,587,891,798,805 | 20% | ||
fishdd | 0 | 934,883,346 | 25.27% | ||
angelinafx | 0 | 4,365,453,975 | 7.16% | ||
garrettwallace | 0 | 1,384,262,038 | 18% | ||
jaydih | 0 | 244,168,378,533 | 5% | ||
killerteesuk | 0 | 896,983,082 | 99% | ||
andrewma | 0 | 5,045,879,993 | 20% | ||
hranhuk | 0 | 6,344,813,891 | 40% | ||
kimseun | 0 | 33,648,872,071 | 5% | ||
mynotsofitlife | 0 | 949,566,053 | 10% | ||
hmayak | 0 | 23,074,924,144 | 100% | ||
archisteem | 0 | 4,716,346,706 | 7.5% | ||
cn-malaysia | 0 | 2,836,239,207 | 10% | ||
merlion | 0 | 136,628,745 | 0.5% | ||
moneybaby | 0 | 820,008,781 | 2.5% | ||
kanadaramagi123 | 0 | 22,898,699,979 | 5% | ||
suonghuynh | 0 | 210,838,906,862 | 7.16% | ||
abcallen | 0 | 17,590,096,684 | 80% | ||
loveecho | 0 | 42,290,169,129 | 5% | ||
bambugrove | 0 | 545,334,997 | 100% | ||
money-way | 0 | 571,544,087 | 100% | ||
angelslake | 0 | 8,681,877,453 | 5% | ||
yestermorrow | 0 | 9,105,673,292 | 31% | ||
dalz | 0 | 7,157,810,004 | 10% | ||
smartvote | 0 | 312,043,083,315 | 7.16% | ||
blues-wclouds | 0 | 1,752,009,336 | 100% | ||
voxmortis | 0 | 52,795,921,229 | 20% | ||
wordit | 0 | 8,390,329,934 | 5% | ||
munhenhos | 0 | 1,960,812,865 | 20% | ||
sj-jeong | 0 | 270,308,939,395 | 5% | ||
wondumyungga | 0 | 18,620,460,786 | 5% | ||
aquawink | 0 | 21,383,510,048 | 5% | ||
jamzmie | 0 | 932,120,231 | 15% | ||
sasaadrian | 0 | 32,601,157,402 | 20% | ||
pollygonewild | 0 | 0 | 10% | ||
ringit | 0 | 14,284,477,632 | 5% | ||
coreabeforekorea | 0 | 174,525,255,948 | 5% | ||
talkative-bk | 0 | 216,775,318,109 | 5% | ||
starrouge | 0 | 1,040,825,961 | 50% | ||
steemegg | 0 | 764,847,410 | 2.5% | ||
cnstm | 0 | 1,011,344,748,287 | 100% | ||
likuang007 | 0 | 40,978,675,922 | 100% | ||
ctime | 0 | 611,991,452,943 | 8% | ||
lianjingmedia | 0 | 1,004,005,509 | 100% | ||
bobdavids | 0 | 0 | 10% | ||
a-secondchance | 0 | 1,240,848,146 | 10% | ||
chocolatelover | 0 | 3,986,643,917 | 40% | ||
instagram-models | 0 | 22,696,351,512 | 4% | ||
hungrybear | 0 | 109,632,529 | 0.5% | ||
electrodo | 0 | 74,831,566,933 | 10% | ||
hungryharish | 0 | 5,623,426,533 | 100% | ||
elektroyazilim | 0 | 607,055,739 | 60% | ||
cnbuddy-cnstm | 0 | 26,436,844,792 | 100% | ||
kryptogames | 0 | 105,489,483,401 | 20% | ||
lovelemon | 0 | 58,600,482,082 | 100% | ||
theinspiration | 0 | 545,507,795 | 100% | ||
epic4chris | 0 | 548,016,184 | 100% | ||
rj-photo | 0 | 1,440,264,040 | 100% | ||
blockchainpeople | 0 | 19,672,023 | 3% | ||
whatdidshewear | 0 | 34,221,645,408 | 5% | ||
sjgod4018 | 0 | 104,596,134,248 | 5% | ||
mustard-seed | 0 | 16,599,482,203 | 5% | ||
morwen | 0 | 18,913,836,642 | 11% | ||
dashkb | 0 | 352,840,003 | 100% | ||
moonhodler | 0 | 1,958,025,929 | 20% | ||
hyborian-strain | 0 | 2,012,298,666 | 30% | ||
perrymine | 0 | 930,113,631 | 5% | ||
wenjiewu | 0 | 98,997,750 | 25% | ||
dw-g8m8k1042298 | 0 | 8,583,050 | 10% | ||
teresa.yaiyai | 0 | 7,980,485,606 | 100% | ||
freedomteam2019 | 0 | 2,892,027,741 | 25% | ||
dw-fn0uh101562u | 0 | 0 | 10% | ||
therealyme | 0 | 3,559,677,447 | 16% | ||
jaykayw | 0 | 11,654,967,392 | 5% | ||
friendlystranger | 0 | 391,564,839,706 | 16% | ||
reward.tier1 | 0 | 596,001,081 | 100% | ||
spx | 0 | 105,443,113,830 | 100% | ||
oblivioncubed | 0 | 540,718,107 | 10% | ||
simply2koool | 0 | 10,587,010,935 | 25% | ||
ribary | 0 | 596,638,475,958 | 33% | ||
asimpleman | 0 | 6,029,743,314 | 100% |
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)
post_id | 85,986,441 |
---|---|
author | resteemsupport |
permlink | re-str-bytes-20200513t030034 |
category | cn |
json_metadata | {} |
created | 2020-05-13 03:00:36 |
last_update | 2020-05-13 03:00:36 |
depth | 1 |
children | 0 |
net_rshares | 0 |
last_payout | 2020-05-20 03:00:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 SBD |
curator_payout_value | 0.000 SBD |
pending_payout_value | 0.000 SBD |
promoted | 0.000 SBD |
body_length | 236 |
author_reputation | -5,666,740,605,819 |
root_title | 每天进步一点点:字符串(str)转字节型数据(bytes)的几种写法 |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 SBD |
percent_steem_dollars | 10,000 |