{"id":2970,"date":"2025-05-31T16:29:13","date_gmt":"2025-06-04T09:17:12","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2970"},"modified":"2025-06-04T16:29:13","modified_gmt":"2025-06-04T09:17:12","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bytes%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bytes%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `bytes()` \u51fd\u5f0f\uff1a\u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, bytes(), \u7a0b\u5f0f\u6559\u5b78, \u4e8c\u9032\u4f4d\u8cc7\u6599, \u7de8\u78bc\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3 Python \u4e2d\u7684 `bytes()` \u51fd\u5f0f\uff1a\u6700\u65b0\u7528\u6cd5\u8207\u6700\u4f73\u5be6\u8e10<\/h1>\n<p>\u5728 Python \u4e2d\uff0c`<strong>bytes()<\/strong>` \u51fd\u5f0f\u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u80fd\u5c07\u5b57\u4e32\u8f49\u63db\u70ba bytes \u7269\u4ef6\u3002\u9019\u4e9b bytes \u7269\u4ef6\u662f\u4e0d\u53ef\u8b8a\u7684\u5e8f\u5217\uff0c\u7279\u5225\u9069\u5408\u7528\u4f86\u5132\u5b58\u539f\u59cb\u7684\u4e8c\u9032\u4f4d\u8cc7\u6599\uff0c\u5982\u5716\u7247\u548c\u97f3\u6a02\u6a94\u6848\u3002\u4ee5\u4e0b\u5c07\u4ecb\u7d39 `bytes()` \u51fd\u5f0f\u7684\u7528\u6cd5\u3001\u61c9\u7528\u7bc4\u4f8b\u53ca\u932f\u8aa4\u6392\u9664\u6280\u5de7\uff0c\u5e6b\u52a9\u4f60\u638c\u63e1\u9019\u500b\u51fd\u5f0f\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>## `bytes()` \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`bytes()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u6216\u5169\u500b\u53c3\u6578\u3002\u7b2c\u4e00\u500b\u53c3\u6578\u662f\u8981\u8f49\u63db\u7684\u7269\u4ef6\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u662f\u7de8\u78bc\u65b9\u5f0f\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5b57\u4e32\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)<\/p>\n<p># \u5c07\u6574\u6578\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes([0x41, 0x42, 0x43])<\/p>\n<p># \u5c07\u5143\u7d44\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes((0x41, 0x42, 0x43))<\/p>\n<p># \u5c07\u53ef\u8fed\u4ee3\u7269\u4ef6\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes([i for i in range(10)])<br \/>\n&#8220;`<\/p>\n<p>## \u5be6\u4f5c\u7bc4\u4f8b\uff1a\u5c07\u5716\u7247\u8f49\u63db\u70ba bytes \u7269\u4ef6<\/p>\n<p>\u5728\u5be6\u969b\u61c9\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u6703\u9700\u8981\u5c07\u5716\u7247\u6a94\u6848\u8f49\u63db\u70ba bytes \u7269\u4ef6\uff0c\u7136\u5f8c\u9032\u884c\u7db2\u8def\u50b3\u8f38\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u4f8b\u5b50\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5716\u7247\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nwith open(&#8220;image.jpg&#8221;, &#8220;rb&#8221;) as f:<br \/>\n    x = f.read()<\/p>\n<p># \u5c07 bytes \u7269\u4ef6\u50b3\u8f38\u5230\u7db2\u8def<br \/>\ns.send(x)<br \/>\n&#8220;`<\/p>\n<p>## `bytes()` \u51fd\u5f0f\u7684\u9032\u968e\u61c9\u7528<\/p>\n<p>\u9664\u4e86\u57fa\u672c\u7684\u8f49\u63db\uff0c`bytes()` \u51fd\u5f0f\u9084\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u8cc7\u6599\u7684\u7de8\u78bc\u548c\u52a0\u5bc6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u61c9\u7528\u5834\u666f\uff1a<\/p>\n<p>### 1. \u5b57\u4e32\u7de8\u78bc<br \/>\n\u5c07\u5b57\u4e32\u8f49\u63db\u70ba UTF-8 \u7de8\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)<br \/>\n&#8220;`<\/p>\n<p>### 2. \u5b57\u4e32\u6bd4\u8f03<br \/>\n\u4f7f\u7528 bytes \u7269\u4ef6\u9032\u884c\u5b57\u4e32\u6bd4\u8f03\uff1a<\/p>\n<p>&#8220;`python<br \/>\nif x == b&#8221;Hello World&#8221;:<br \/>\n    print(&#8220;The strings are equal&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### 3. \u8cc7\u6599\u52a0\u5bc6<br \/>\n\u5c07\u5b57\u4e32\u52a0\u5bc6\u6210 SHA-256 \u7de8\u78bc\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport hashlib<\/p>\n<p>x = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)<br \/>\nx = hashlib.sha256(x).digest()<\/p>\n<p># \u6aa2\u67e5\u8cc7\u6599\u7684\u5b89\u5168\u6027<br \/>\nif x == b&#8221;\\x12\\x34\\x56\\x78\\x90\\xab\\xcd\\xef&#8221;:<br \/>\n    print(&#8220;The data is secure&#8221;)<br \/>\n&#8220;`<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<br \/>\n\u5728\u4f7f\u7528 `bytes()` \u51fd\u5f0f\u6642\uff0c\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u554f\u984c\uff1a<br \/>\n&#8211; **TypeError**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u8cc7\u6599\u985e\u578b\u6b63\u78ba\u3002\u4f8b\u5982\uff0c\u4e0d\u80fd\u76f4\u63a5\u5c07\u5b57\u5178\u6216\u5176\u4ed6\u4e0d\u652f\u6301\u7684\u985e\u578b\u50b3\u5165\u3002<br \/>\n&#8211; **UnicodeEncodeError**\uff1a\u7576\u4f7f\u7528\u4e0d\u652f\u6301\u7684\u7de8\u78bc\u6642\uff0c\u78ba\u4fdd\u7de8\u78bc\u65b9\u5f0f\u6b63\u78ba\uff0c\u5982 `utf-8`\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<br \/>\n`bytes()` \u51fd\u5f0f\u4e0d\u50c5\u9650\u65bc\u5b57\u4e32\u8655\u7406\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u7db2\u8def\u7a0b\u5f0f\u8a2d\u8a08\u3001\u8cc7\u6599\u5eab\u5132\u5b58\u548c\u8cc7\u6599\u50b3\u8f38\u3002\u4e86\u89e3\u9019\u4e9b\u61c9\u7528\u80fd\u63d0\u5347\u4f60\u7684\u7a0b\u5f0f\u8a2d\u8a08\u80fd\u529b\u3002<\/p>\n<p>\u60f3\u8981\u4e86\u89e3\u66f4\u591a Python \u7684\u61c9\u7528\uff0c\u8acb\u53c3\u8003\u6211\u5011\u7684[Python \u6559\u5b78\u7cfb\u5217](https:\/\/vocus.cc)\u3002<\/p>\n<p>&#8212;<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: `bytes()` \u51fd\u5f0f\u53ef\u4ee5\u7528\u65bc\u54ea\u4e9b\u8cc7\u6599\u985e\u578b\uff1f**<br \/>\nA1: `bytes()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u5b57\u4e32\u3001\u6574\u6578\u3001\u5143\u7d44\u53ca\u5176\u4ed6\u53ef\u8fed\u4ee3\u7269\u4ef6\u4f5c\u70ba\u53c3\u6578\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c07 bytes \u7269\u4ef6\u8f49\u56de\u5b57\u4e32\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `decode()` \u65b9\u6cd5\u4f86\u5c07 bytes \u7269\u4ef6\u8f49\u63db\u56de\u5b57\u4e32\uff0c\u4f8b\u5982 `x.decode(&#8216;utf-8&#8217;)`\u3002<\/p>\n<p>**Q3: `bytes()` \u8207 `bytearray()` \u6709\u4f55\u4e0d\u540c\uff1f**<br \/>\nA3: `bytes()` \u751f\u6210\u4e0d\u53ef\u8b8a\u7684 bytes \u7269\u4ef6\uff0c\u800c `bytearray()` \u5247\u751f\u6210\u53ef\u8b8a\u7684 bytes \u7269\u4ef6\uff0c\u9019\u4f7f\u5f97\u5f8c\u8005\u53ef\u4ee5\u9032\u884c\u4fee\u6539\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684Bytes()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5b57\u7bc0\u5e8f\u5217\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u65b9\u4fbf\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u5b57\u7bc0\u5e8f\u5217\u3002\u672c\u6587\u5c07\u4ecb\u7d39Bytes()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u64cd\u4f5c\u5b57\u7bc0\u5e8f\u5217\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-2970","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-LU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2970","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2970"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2970\/revisions"}],"predecessor-version":[{"id":12996,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2970\/revisions\/12996"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2970"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2970"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2970"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}