繁轉簡Function
繁體轉簡體Function
//做法一:此做法會將BIG5轉為GBK並對應到簡體字
function Big5_To_GB(BIG5Str: String): AnsiString;
var
Len: Integer;
pBIG5Char: PChar;
pGBCHSChar: PChar;
pGBCHTChar: PChar;
pUniCodeChar: PWideChar;
begin
//String -> PChar
pBIG5Char := PChar(BIG5Str);
Len := MultiByteToWideChar(950,0,pBIG5Char,-1,nil,0);
GetMem(pUniCodeChar,Len*2);
ZeroMemory(pUniCodeChar,Len*2);
//Big5 -> UniCode
MultiByteToWideChar(950,0,pBIG5Char,-1,pUniCodeChar,Len);
Len := WideCharToMultiByte(936,0,pUniCodeChar,-1,nil,0,nil,nil);
GetMem(pGBCHTChar,Len*2);
GetMem(pGBCHSChar,Len*2);
ZeroMemory(pGBCHTChar,Len*2);
ZeroMemory(pGBCHSChar,Len*2);
//UniCode->GB CHT
WideCharToMultiByte(936,0,pUniCodeChar,-1,pGBCHTChar,Len,nil,nil);
//GB CHT -> GB CHS
LCMapString($804,LCMAP_SIMPLIFIED_CHINESE,pGBCHTChar,-1,pGBCHSChar,Len);
Result := String(pGBCHSChar);
FreeMem(pGBCHTChar);
FreeMem(pGBCHSChar);
FreeMem(pUniCodeChar);
end;
//作法二:此做法會將BIG5轉為GBK但會對應到GBK的繁體字
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;
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 UnicodeDecode(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 UnicodeEncode(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;
沒有留言:
張貼留言