编码转换

来源:互联网 发布:河南软件服务协会 编辑:程序博客网 时间:2024/05/19 02:04

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 tolength(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 1do
     begin
      i:=pos(hex[loop],cHex)-1;
     if (i<0) then i:=0;
       inc(result,(i*mult));
      mult:=mult*16;
     end;
end;




Delphi: 将汉字转换为Unicode码
使用api函数中MultiByteToWidechar

functionUnicodeEncode(Str:string;CodePage:integer):WideString;

var
  Len:integer;
begin
  Len:=Length(Str)+1;
  SetLength(Result,Len);
  Len:=MultiByteToWideChar(CodePage,0,PChar(Str),-1,PWideChar(Result),Len);
  SetLength(Result,Len-1); //end is #0
end;

functionUnicodeDecode(Str:WideString;CodePage:integer):string;
var
  Len:integer;
begin
  Len:=Length(Str)*2+1; //one for #0
  SetLength(Result,Len);
  Len:=WideCharToMultiByte(CodePage,0,PWideChar(Str),-1,PChar(Result),Len,nil,nil);
  SetLength(Result,Len-1);
end;

function Gb2Big5(Str:string):string;
begin
  SetLength(Result,Length(Str));
  LCMapString(GetUserDefaultLCID,LCMAP_TRADITIONAL_CHINESE,
  PChar(Str),Length(Str),
  PChar(Result),Length(Result));
  Result:=UnicodeDecode(UnicodeEncode(Result,936),950);
end;

function Big52Gb(Str:string):string;
begin
  Str:=UnicodeDecode(UnicodeEncode(Str,950),936);
  SetLength(Result,Length(Str));
  LCMapString(GetUserDefaultLCID,LCMAP_SIMPLIFIED_CHINESE,
    PChar(Str),Length(Str),
   PChar(Result),Length(Result));
end;

关键使用了UnicodeToUtf8这个函数

function Utf8Encode(const WS: WideString): UTF8String;

var
  L: Integer;
  Temp: UTF8String;
begin
  Result := '';
  if WS = '' then Exit;
  SetLength(Temp, Length(WS) * 3); // SetLengthincludes space for null terminator
  L := UnicodeToUtf8(PChar(Temp), Length(Temp)+1,PWideChar(WS), Length(WS));
  if L > 0 then
    SetLength(Temp, L-1)
  else
    Temp := '';
  Result := Temp;
end;

原创粉丝点击