{"id":4298,"date":"2016-11-08T12:23:29","date_gmt":"2016-11-08T04:23:29","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=4298"},"modified":"2016-11-12T13:39:11","modified_gmt":"2016-11-12T05:39:11","slug":"%e6%9d%82%e8%b0%88%e4%bd%bf%e7%94%a8-gb2312-%e7%bc%96%e7%a0%81%e8%bd%ac%e4%b9%89url%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=4298","title":{"rendered":"[\u6742\u8c08]\u4f7f\u7528 GB2312 \u7f16\u7801\u8f6c\u4e49URL\u5b57\u7b26\u4e32"},"content":{"rendered":"<h4>\u3010\u66f4\u65b0\u3011<\/h4>\n<p>2016.11.12<\/p>\n<p style=\"padding-left: 30px;\">QString \u65b0\u589e\u4e86 UrlEncode \u51fd\u6570\uff0c\u4f18\u5316\u4e86\u6027\u80fd\uff0c\u901f\u5ea6\u6bd4\u5b98\u65b9\u7684\u7248\u672c\u5feb\u4e00\u70b9\uff0c\u66f4\u91cd\u8981\u7684\u662f\u53ef\u4ee5ANSI\u548cUTF8\u7f16\u7801\uff0c\u6216\u8005\u662f\u81ea\u5df1\u559c\u6b22\u7684\u7f16\u7801\u90fd\u884c\u3002<\/p>\n<pre class=\"lang:delphi decode:true\" style=\"padding-left: 30px;\">function UrlEncode(const ABytes: PByte; l: Integer; ASpacesAsPlus: Boolean)\r\n  : QStringW; overload;\r\nfunction UrlEncode(const ABytes: TBytes; ASpacesAsPlus: Boolean)\r\n  : QStringW; overload;\r\nfunction UrlEncode(const S: QStringW; ASpacesAsPlus: Boolean;\r\n  AUtf8Encode: Boolean = True): QStringW; overload;<\/pre>\n<p style=\"padding-left: 30px;\">\u4e09\u4e2a\u91cd\u8f7d\uff0c\u524d\u4e24\u4e2a\u65b9\u4fbf\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u7f16\u7801\u7684\uff0c\u7b2c\u4e09\u4e2a\u91cd\u8f7d\u5982\u679c\u4e0d\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5c31\u4f7f\u7528 ANSI \u7f16\u7801\u6765\u5904\u7406\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u4ee3\u7801\u662f\u76f4\u63a5\u6539\u7684\u7cfb\u7edf\u7684TURI.EncodeUrl\uff0c\u53ea\u662f\u5c06\u9ed8\u8ba4\u7684UTF-8\u7f16\u7801\u66f4\u6362\u4e3a\u4e86Ansi<\/p>\n<pre class=\"lang:delphi decode:true \">function AnsiURLEncode(const AValue: string; SpacesAsPlus: Boolean): string;\r\n\r\n  function IsHexChar(C: Byte): Boolean;\r\n  begin\r\n    case Char(C) of\r\n      '0' .. '9', 'a' .. 'f', 'A' .. 'F':\r\n        Result := True;\r\n    else\r\n      Result := False;\r\n    end;\r\n  end;\r\n\r\n\/\/ from http:\/\/www.faqs.org\/rfcs\/rfc3986.html\r\n\/\/ URL safe chars = ['A' .. 'Z', 'a' .. 'z', '0' .. '9', '-', '_', '.', '~'];\r\nconst\r\n  URLSafeCharMatrix: array [33 .. 127] of Boolean = (False, False, False, False,\r\n    False, False, False, False, False, False, False, False, True, True, False,\r\n    True, True, True, True, True, True, True, True, True, True, False, False,\r\n    False, False, False, False, False, True, True, True, True, True, True, True,\r\n    True, True, True, True, True, True, True, True, True, True, True, True,\r\n    True, True, True, True, True, True, True, False, False, False, False, True,\r\n    False, True, True, True, True, True, True, True, True, True, True, True,\r\n    True, True, True, True, True, True, True, True, True, True, True, True,\r\n    True, True, True, False, False, False, True, False);\r\n\r\n  XD: array [0 .. 15] of Char = ('0', '1', '2', '3', '4', '5', '6', '7', '8',\r\n    '9', 'A', 'B', 'C', 'D', 'E', 'F');\r\nvar\r\n  Buff: TBytes;\r\n  I: Integer;\r\nbegin\r\n  Buff := TEncoding.ANSI.GetBytes(AValue);\r\n  Result := '';\r\n  I := 0;\r\n  while I &lt; Length(Buff) do\r\n  begin\r\n    if (I + 2 &lt; Length(Buff)) and (Buff[I] = Ord('%')) then\r\n      if IsHexChar(Buff[I + 1]) and IsHexChar(Buff[I + 2]) then\r\n      begin\r\n        Result := Result + '%' + Char(Buff[I + 1]) + Char(Buff[I + 2]);\r\n        Inc(I, 3);\r\n        Continue;\r\n      end;\r\n\r\n    if (Buff[I] = Ord(' ')) and SpacesAsPlus then\r\n      Result := Result + '+'\r\n    else\r\n    begin\r\n      if (Buff[I] &gt;= 33) and (Buff[I] &lt;= 127) then\r\n      begin\r\n        if URLSafeCharMatrix[Buff[I]] then\r\n          Result := Result + Char(Buff[I])\r\n        else\r\n          Result := Result + '%' + XD[(Buff[I] shr 4) and $0F] +\r\n            XD[Buff[I] and $0F];\r\n      end\r\n      else\r\n        Result := Result + '%' + XD[(Buff[I] shr 4) and $0F] +\r\n          XD[Buff[I] and $0F];\r\n    end;\r\n    Inc(I);\r\n  end;\r\nend;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u66f4\u65b0\u3011 2016.11.12 QStr [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[],"class_list":["post-4298","post","type-post","status-publish","format-standard","hentry","category-delphi"],"views":6731,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4298","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4298"}],"version-history":[{"count":3,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4298\/revisions"}],"predecessor-version":[{"id":4301,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4298\/revisions\/4301"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}