{"id":5074,"date":"2023-01-05T11:50:18","date_gmt":"2023-01-05T03:50:18","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5074"},"modified":"2023-01-05T11:50:18","modified_gmt":"2023-01-05T03:50:18","slug":"%e5%ad%b8%e7%bf%92typescript%e5%ad%97%e4%b8%b2%e7%9a%84unicode%e7%b7%a8%e7%a2%bcutf8encode","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e5%ad%b8%e7%bf%92typescript%e5%ad%97%e4%b8%b2%e7%9a%84unicode%e7%b7%a8%e7%a2%bcutf8encode\/","title":{"rendered":"\u5b78\u7fd2TypeScript\u5b57\u4e32\u7684Unicode\u7de8\u78bc(utf8Encode)"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, Unicode, utf8Encode\"><\/p>\n<h1>TypeScript \u5b57\u4e32\u7684 Unicode \u7de8\u78bc(utf8Encode)<\/h1>\n<p>TypeScript \u662f\u4e00\u7a2e JavaScript \u7684\u8d85\u96c6\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u985e\u578b\u6aa2\u67e5\u548c\u7de8\u8b6f\u6642\u7684\u932f\u8aa4\u6aa2\u67e5\u3002\u5b83\u4e5f\u652f\u6301 Unicode \u7de8\u78bc\uff0c\u53ef\u4ee5\u5c07\u5b57\u4e32\u7de8\u78bc\u70ba UTF-8 \u683c\u5f0f\uff0c\u4ee5\u4fbf\u5728\u4e0d\u540c\u7684\u7cfb\u7d71\u4e0a\u4f7f\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5728 TypeScript \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <span style=\"font-weight: bold;\">utf8Encode<\/span> \u51fd\u6578\u5c07\u5b57\u4e32\u7de8\u78bc\u70ba UTF-8 \u683c\u5f0f\u3002\u5b83\u63a5\u53d7\u4e00\u500b\u5b57\u4e32\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u7de8\u78bc\u5f8c\u7684\u5b57\u4e32\u3002<\/p>\n<pre class=\"brush: typescript\">\nfunction utf8Encode(str: string): string {\n    let encodedStr = \"\";\n    for (let i = 0; i < str.length; i++) {\n        let charCode = str.charCodeAt(i);\n        if (charCode < 0x80) {\n            encodedStr += String.fromCharCode(charCode);\n        } else if (charCode < 0x800) {\n            encodedStr += String.fromCharCode(0xc0 | (charCode >> 6));\n            encodedStr += String.fromCharCode(0x80 | (charCode & 0x3f));\n        } else if (charCode < 0xd800 || charCode >= 0xe000) {\n            encodedStr += String.fromCharCode(0xe0 | (charCode >> 12));\n            encodedStr += String.fromCharCode(0x80 | ((charCode >> 6) & 0x3f));\n            encodedStr += String.fromCharCode(0x80 | (charCode & 0x3f));\n        } else {\n            \/\/ surrogate pair\n            i++;\n            \/\/ UTF-16 encodes 0x10000-0x10FFFF by\n            \/\/ subtracting 0x10000 and splitting the\n            \/\/ 20 bits of 0x0-0xFFFFF into two halves\n            charCode = 0x10000 + (((charCode & 0x3ff) << 10)\n                          | (str.charCodeAt(i) &#038; 0x3ff));\n            encodedStr += String.fromCharCode(0xf0 | (charCode >> 18));\n            encodedStr += String.fromCharCode(0x80 | ((charCode >> 12) & 0x3f));\n            encodedStr += String.fromCharCode(0x80 | ((charCode >> 6) & 0x3f));\n            encodedStr += String.fromCharCode(0x80 | (charCode & 0x3f));\n        }\n    }\n    return encodedStr;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u6578\u6703\u5c07\u5b57\u4e32\u7de8\u78bc\u70ba UTF-8 \u683c\u5f0f\uff0c\u4e26\u8fd4\u56de\u7de8\u78bc\u5f8c\u7684\u5b57\u4e32\u3002<\/p>\n<p>\u8981\u4f7f\u7528\u9019\u500b\u51fd\u6578\uff0c\u53ea\u9700\u8981\u5c07\u5b57\u4e32\u4f5c\u70ba\u53c3\u6578\u50b3\u905e\u7d66\u5b83\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet encodedStr = utf8Encode(\"Hello World!\");\n<\/pre>\n<p>\u9019\u6a23\u5c31\u53ef\u4ee5\u5c07\u5b57\u4e32\u7de8\u78bc\u70ba UTF-8 \u683c\u5f0f\uff0c\u4e26\u8fd4\u56de\u7de8\u78bc\u5f8c\u7684\u5b57\u4e32\u3002<\/p>\n<p>TypeScript \u7684 Unicode \u7de8\u78bc\u529f\u80fd\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7cfb\u7d71\u4e0a\u4f7f\u7528\u5b57\u4e32\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3\u5b57\u4e32\u7684\u7de8\u78bc\u554f\u984c\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5728\u4e0d\u540c\u7684\u7cfb\u7d71\u4e0a\u4f7f\u7528\u5b57\u4e32\uff0c\u800c\u4e0d\u7528\u64d4\u5fc3\u5b57\u4e32\u7684\u7de8\u78bc\u554f\u984c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2TypeScript\u5b57\u4e32\u7684Unicode\u7de8\u78bc(utf8Encode)\uff0c\u53ef\u4ee5\u8b93\u4f60\u66f4\u6709\u6548\u7387\u5730\u5c0d\u5b57\u4e32\u9032\u884c\u7de8\u78bc\uff0c\u4e26\u66f4\u597d\u5730\u7ba1\u7406\u5b57\u4e32\u8cc7\u6599\u3002\u672c\u6587\u5c07\u4ecb\u7d39TypeScript\u4e2d\u7684utf8Encode\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5c0d\u5b57\u4e32\u9032\u884c\u7de8\u78bc\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"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":[187],"tags":[186],"class_list":["post-5074","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1jQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5074","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=5074"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5074\/revisions"}],"predecessor-version":[{"id":5075,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5074\/revisions\/5075"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}