Delphi UniCode转汉字(u 格式)、汉字转UniCode(u 格式)

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 := 'u6ed4u0052u006fu0079';
  
  s:=ChineseToUniCode(s1);  {汉字到 UniCode 编码}
  s:=UnicodeToChinese(s2);  { UniCode 编码到汉字}  
end;

  

  

创建时间:2021.05.04  更新时间:

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/14729102.html