Delphi UniCode轉漢字(\u 格式)、漢字轉UniCode(\u 格式)
1、UniCode轉漢字
function UnicodeToChinese(sStr: string): string;
var
i: Integer;
index: Integer;
temp, top, last: string;
begin
index := 1;
while index >= 0 do
begin
index := Pos('\u', sStr) - 1;
if index < 0 then //非 unicode編碼不轉換 ,自動過濾
begin
last := sStr;
Result := Result + last;
Exit;
end;
top := Copy(sStr, 1, index); // 取出 編碼字符前的 非 unic 編碼的字符,如數字
temp := Copy(sStr, index + 1, 6); // 取出編碼,包括 \u,如\u4e3f
Delete(temp, 1, 2);
Delete(sStr, 1, index + 6);
Result := Result + top + WideChar(StrToInt('$' + temp));
end;
end;
2、漢字轉UniCode
function ChineseToUniCode(sStr: string): string; //漢字的 UniCode 編碼范圍是: $4E00..$9FA5 作者:滔Roy
var
w:Word;
hz:WideString;
i:Integer;
s:string;
begin
hz:=sStr;
for i:=1 to Length(hz) do begin
w := Ord(hz[i]);
s:=IntToHex(w, 4);
Result := Result +'\u'+ LowerCase(s);
end;
end;
3、示例:
var
s,s1,s2 : string;
begin
s1 := '滔Roy';
s2 := '\u6ed4\u0052\u006f\u0079';
s:=ChineseToUniCode(s1); {漢字到 UniCode 編碼}
s:=UnicodeToChinese(s2); { UniCode 編碼到漢字}
end;
創建時間:2021.05.04 更新時間: