频道分类

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

作者:admin 来源: 日期:2020/4/4 18:35:58 人气: 标签:

 
</pre><pre code_snippet_id="76206" snippet_file_name="blog_20131121_2_2675795" name="code" class="javascript">{"result":["{\"ServerCount\":2,\"Servers\":[{\"ServerName\":\"\u672C\u573014\u670D\",\"ServerIP\":\"172.16.228.14:8000\"},{\"ServerName\":\"\u7535\u4FE1\u6D4B\u8BD5\u670D\",\"ServerIP\":\"12.23.34.45:8000\"}]}\r\n"]}


转换后效果



{"result":["{"servercount":2,"servers":[{"servername":"本地14服","serverip":"172.16.228.14:8000"},{"servername":"电信测试服","serverip":"12.23.34.45:8000"}]}
"]}








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;


————————————————

原文链接:https://blog.csdn.net/warrially/article/details/16862853

上一篇:Delphi Base32 的加密和解密下一篇:没有资料