Delphi: 将unicode码转换为汉字

时间:2021-12-26 08:42:00

Delphi: 将unicode码转换为汉字
Night @ 2004-12-01 21:12

function UnicodeToAnsi(SubUnicode: string):string;  //将unicode码转换为汉字
   var a:array[0..500] of char;
      s1,s2:char;
      substr1,substr2,s:string;
      str:string;
      i:integer;
   begin
     if length(SubUnicode) mod 4 = 0 then
     Begin



       str:='';
       for i:=1 to length(SubUnicode) div 4 do
       Begin
         s:='';
         substr1:=copy(SubUnicode,i*4-3,2);
         substr2:=copy(SubUnicode,i*4-1,2);
         s1:=chr(hextoint(substr1));
         s2:=chr(hextoint(substr2));
         s:=s+s2+s1;
         strpcopy(a,s);
         str:=str+copy(widechartostring(@(a[0])),1,2);
       end;
       result:=str;
     end;
   end;



function HexToInt(hex:string):cardinal;
const cHex='0123456789ABCDEF';
var mult,i,loop:integer;
begin
      result:=0;
      mult:=1;
      for loop:=length(hex)downto 1 do
      begin
       i:=pos(hex[loop],cHex)-1;
       if (i<0) then i:=0;
       inc(result,(i*mult));
       mult:=mult*16;
      end;
end;