Delphi REST 服务器返回UTF16编码转换成正常string

来源:互联网 发布:淘宝延长收货会怎样 编辑:程序博客网 时间:2024/05/16 03:54
json  UTF16 TO GB2312
</pre><pre name="code" class="delphi">
<pre name="code" class="delphi">function wwChangeUTF8ToWideString(szJson :string):string;function wwChangeUTF8ToWideString(szJson :string):string;  function XDigit(Ch : AnsiChar) : Integer;  begin    if (Ch >= '0') and (Ch <= '9') then        Result := Ord(Ch) - Ord('0')    else        Result := (Ord(Ch) and 15) + 9;  end;  function wwUtfToString(szUtf: string):string;  var    I:Integer;    Index:Integer;    WChar:WideChar;    WCharWord:Word;    AChar:AnsiChar;  begin    WCharWord:=0;    for i := 1 to Length(szUtf) do      begin        AChar := AnsiChar(szUtf[i]);        WCharWord := WCharWord + XDigit(AChar) * Ceil(Power(16,4-i));      end;    WChar := WideChar(WCharWord);    Result := WChar;  end;var  Index:Integer;  HexStr:String;begin  szJson := LowerCase(szJson);  szJson := StringReplace(szJson, '\"', '"', [rfReplaceAll]);  szJson := StringReplace(szJson, '\r', #10, [rfReplaceAll]);  szJson := StringReplace(szJson, '\n', #13, [rfReplaceAll]);  szJson := StringReplace(szJson, '\\', '\', [rfReplaceAll]);  Index := PosEx('\u',szJson,1);  while Index>0 do  begin    HexStr:=Copy(szJson,Index+2,4);    wwUtfToString(HexStr);    szJson := StringReplace(szJson, '\u'+HexStr, wwUtfToString(HexStr),[rfReplaceAll]);    Index:=PosEx('\u',szJson,1);  end;  Result := szJson; // byWarrially end;


                                             
0 0