{"id":113,"date":"2024-10-31T01:02:29","date_gmt":"2024-10-31T01:02:29","guid":{"rendered":"https:\/\/burningthepasttvt.damo.sg\/?p=113"},"modified":"2024-10-31T01:02:29","modified_gmt":"2024-10-31T01:02:29","slug":"%e5%9f%ba%e6%9c%ac%e7%9f%a5%e8%af%861","status":"publish","type":"post","link":"https:\/\/burningthepasttvt.damo.sg\/?p=113","title":{"rendered":"\u57fa\u672c\u77e5\u8bc61"},"content":{"rendered":"\n<p>urlencode<\/p>\n\n\n\n<p>urlencode \u662f\u4e00\u79cd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a URL \u5b89\u5168\u683c\u5f0f\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5bf9 URL \u4e2d\u7684\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u7f16\u7801\uff0c\u4f7f\u5176\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u3002\u4f8b\u5982\uff0c\u5728 URL \u4e2d\uff0c\u7a7a\u683c\u4f1a\u88ab\u7f16\u7801\u4e3a %20\uff0c\u800c &amp; \u4f1a\u88ab\u7f16\u7801\u4e3a %26\u3002<\/p>\n\n\n\n<p>\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 urllib.parse \u6a21\u5757\u7684 quote \u548c urlencode \u51fd\u6570\u6765\u8fdb\u884c URL \u7f16\u7801\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5355\u4e2a\u5b57\u7b26\u4e32\u7f16\u7801\uff1a<\/li>\n<\/ol>\n\n\n\n<p>from urllib.parse import quote<\/p>\n\n\n\n<p>text = &#8220;Hello World!&#8221;<br>encoded_text = quote(text)<br>print(encoded_text) # \u8f93\u51fa: Hello%20World%21<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5b57\u5178\u7f16\u7801\uff08\u7528\u4e8e URL \u53c2\u6570\uff09\uff1a<\/li>\n<\/ol>\n\n\n\n<p>from urllib.parse import urlencode<\/p>\n\n\n\n<p>params = {&#8216;name&#8217;: &#8216;John Doe&#8217;, &#8216;age&#8217;: 25, &#8216;city&#8217;: &#8216;New York&#8217;}<br>encoded_params = urlencode(params)<br>print(encoded_params) # \u8f93\u51fa: name=John+Doe&amp;age=25&amp;city=New+York<\/p>\n\n\n\n<p>\u5728\u8fd9\u79cd\u7f16\u7801\u683c\u5f0f\u4e0b\uff0c\u7279\u6b8a\u5b57\u7b26\u4f1a\u88ab\u8f6c\u6362\u6210\u201c%\u201d\u52a0\u4e0a\u4e24\u4e2a16\u8fdb\u5236\u7684\u6570\u5b57\u7684\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u5b83\u4eec\u5728 URL \u4e2d\u80fd\u591f\u88ab\u6b63\u786e\u4f20\u9012\u3002<\/p>\n\n\n\n<p>hex encoding<\/p>\n\n\n\n<p>Hex\uff08\u5341\u516d\u8fdb\u5236\uff09\u7f16\u7801\u662f\u4e00\u79cd\u5c06\u6570\u636e\u8868\u793a\u4e3a\u5341\u516d\u8fdb\u5236\u683c\u5f0f\u7684\u7f16\u7801\u65b9\u6cd5\u3002\u901a\u5e38\u5728\u5904\u7406\u5b57\u8282\u6570\u636e\u65f6\u4f7f\u7528\uff0c\u4ee5\u4fbf\u4ee5\u53ef\u8bfb\u7684\u65b9\u5f0f\u8868\u793a\u4e8c\u8fdb\u5236\u6570\u636e\u3002\u6bcf\u4e2a\u5b57\u8282\u7684\u6570\u636e\u7528\u4e24\u4e2a\u5341\u516d\u8fdb\u5236\u5b57\u7b26\u6765\u8868\u793a\u3002\u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 hex() \u65b9\u6cd5\u6216 binascii \u6a21\u5757\u6765\u8fdb\u884c hex \u7f16\u7801\u3002\u4f7f\u7528\u65b9\u6cd51. \u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u7f16\u7801\uff1atext = &#8220;Hello&#8221;<\/p>\n\n\n\n<p>hex_encoded = text.encode(&#8220;utf-8&#8221;).hex()<\/p>\n\n\n\n<p>print(hex_encoded)  # \u8f93\u51fa: 48656c6c6f2.<\/p>\n\n\n\n<p> \u5c06\u5b57\u8282\u6570\u636e\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u7f16\u7801\uff1abyte_data = b&#8221;Hello&#8221;<\/p>\n\n\n\n<p>hex_encoded = byte_data.hex()<\/p>\n\n\n\n<p>print(hex_encoded)  # \u8f93\u51fa: 48656c6c6f3. <\/p>\n\n\n\n<p>\u5c06\u5341\u516d\u8fdb\u5236\u89e3\u7801\u4e3a\u5b57\u7b26\u4e32\uff1ahex_encoded = &#8220;48656c6c6f&#8221;<\/p>\n\n\n\n<p>decoded_text = bytes.fromhex(hex_encoded).decode(&#8220;utf-8&#8221;)<\/p>\n\n\n\n<p>print(decoded_text)  # \u8f93\u51fa: Hello<\/p>\n\n\n\n<p>\u4f7f\u7528 binascii \u6a21\u5757binascii \u6a21\u5757\u63d0\u4f9b\u4e86\u66f4\u591a\u7684 hex \u7f16\u7801\u548c\u89e3\u7801\u529f\u80fd\u3002import binascii<\/p>\n\n\n\n<p># \u7f16\u7801text = &#8220;Hello&#8221;hex_encoded = binascii.hexlify(text.encode(&#8220;utf-8&#8221;))<\/p>\n\n\n\n<p>print(hex_encoded)  # \u8f93\u51fa: b&#8217;48656c6c6f&#8217;<\/p>\n\n\n\n<p># \u89e3\u7801decoded_text = binascii.unhexlify(hex_encoded).decode(&#8220;utf-8&#8221;)<\/p>\n\n\n\n<p>print(decoded_text)  # \u8f93\u51fa: HelloHex \u7f16\u7801\u5e38\u7528\u4e8e\u7f51\u7edc\u4f20\u8f93\u548c\u6570\u636e\u5b58\u50a8\u4e2d\uff0c\u4f7f\u4e8c\u8fdb\u5236\u6570\u636e\u66f4\u5bb9\u6613\u9605\u8bfb\u548c\u4f20\u9012\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>urlencode urlencode \u662f\u4e00\u79cd\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a URL \u5b89\u5168\u683c\u5f0f\u7684\u65b9\u6cd5\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u5bf9 URL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=113"}],"version-history":[{"count":1,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":114,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/113\/revisions\/114"}],"wp:attachment":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}