uses SysUtils, Windows;function StrConv(pszSrc : PChar; cpSrc : UINT; pszDest : PChar; cbDest : Longint; cpDest : UINT) : Longint; var buf : PWideChar; cwLen : LongInt; begin cwLen := MultiByteToWideChar(cpSrc, 0, pszSrc, -1, nil, 0); GetMem(buf, (cwLen+1) * SizeOf(WideChar)); ZeroMemory(buf, (cwLen+1) * SizeOf(WideChar)); MultiByteToWideChar(cpSrc, 0, pszSrc, StrLen(pszSrc), buf, cwLen+1); Result := WideCharToMultiByte(cpDest, 0, buf, -1, pszDest, cbDest, nil, nil); FreeMem(buf); end;function StringConv(const Src : string; cpSrc, cpDest : UINT) : string; var buf : PChar; len : Longint; begin len := StrConv(PChar(Src), cpSrc, nil, 0, cpDest); GetMem(buf, len); ZeroMemory(buf, len); StrConv(PChar(Src), cpSrc, buf, len, cpDest); Result := string(buf); FreeMem(buf); end;function CHTStringToCHSString(Src : string) : string; var buf : PChar; len : Integer; begin len := LCMapString($804, // LCID_CHINESE_SIMPLIFIED LCMAP_SIMPLIFIED_CHINESE, PChar(Src), -1, nil, 0); GetMem(buf, len); ZeroMemory(buf, len); LCMapString($804, // LCID_CHINESE_SIMPLIFIED LCMAP_SIMPLIFIED_CHINESE, PChar(Src), -1, buf, len); Result := string(buf); FreeMem(buf); end;function CHSStringToCHTString(Src : string) : string; var buf : PChar; len : Integer; begin len := LCMapString($404, // LCID_CHINESE_SIMPLIFIED LCMAP_TRADITIONAL_CHINESE, PChar(Src), -1, nil, 0); GetMem(buf, len); ZeroMemory(buf, len); LCMapString($404, // LCID_CHINESE_SIMPLIFIED LCMAP_TRADITIONAL_CHINESE, PChar(Src), -1, buf, len); Result := string(buf); FreeMem(buf); end;function CHTToCHS(pszSrc, pszDest : PChar; cbDest : Longint) : Longint; begin Result := LCMapString($804, // LCID_CHINESE_SIMPLIFIED LCMAP_SIMPLIFIED_CHINESE, pszSrc, -1, pszDest, cbDest); end;function CHSToCHT(pszSrc, pszDest : PChar; cbDest : Longint) : Longint; begin Result := LCMapString($404, // LCID_CHINESE_SIMPLIFIED LCMAP_TRADITIONAL_CHINESE, pszSrc, -1, pszDest, cbDest); end;function Big5StringToGBString(Src : string) : string; begin Result := CHTStringToCHSString(StringConv(Src, 950, 936)); end;function Big5ToGB(pszSrc, pszDest : PChar; cbDest : Longint) : Longint; var buf : PChar; len : Integer; begin len := StrLen(pszSrc)+1; GetMem(buf, len*2); ZeroMemory(buf, len*2); StrConv(pszSrc, 950, buf, 936, len*2); Result := CHTToCHS(buf, pszDest, cbDest); FreeMem(buf); end;function GBStringToBig5String(Src : string) : string; begin Result := CHSStringToCHTString(StringConv(Src, 936, 950)); end;function GBToBig5(pszSrc, pszDest : PChar; cbDest : Longint) : Longint; var buf : PChar; len : Integer; begin len := StrLen(pszSrc)+1; GetMem(buf, len*2); ZeroMemory(buf, len*2); StrConv(pszSrc, 936, buf, 950, len*2); Result := CHSToCHT(buf, pszDest, cbDest); FreeMem(buf); end;
unit CVCode;interface function GBtoBIG5(value: string): string; function BIG5toGB(value: string): string;implementationvar BIG5Order: array[0..14757] of Word; GBOrder : array[0..8177] of Word;function GBOffset(value: string): integer; begin if length(value) >= 2 then Result := (Ord(value[1]) - 161) * 94 + (Ord(value[2]) - 161) else Result := -1; end;function BIG5Offset(value: string): integer; begin Result := -1; if length(value) >= 2 then begin if (Ord(value[2]) >= 64) and (Ord(value[2]) <= 126) then Result := (Ord(value[1]) - 161) * 157 + (Ord(value[2]) - 64); if (Ord(value[2]) >= 161) and (Ord(value[2]) <= 254) then Result := (Ord(value[1]) - 161) * 157 + 63 + (Ord(value[2]) - 161); end end;function WordToString(value: Word): string; begin Result := Chr(Hi(value)) + Chr(Lo(Value)); end;function isBIG5(value: string): Boolean; begin if (length(value)>=2) then begin if (value[1] < #161) then Result := false else if ((value[2] >= #64) and (value[2] <= #126)) or ((value[2] >= #161) and (value[2] <= #254)) then Result := true else Result := false end else Result := false end;function isGB(value: string): Boolean; begin if (length(value)>=2) then begin if (value[1] <= #161) and (value[1] >= #247) then Result := false else if (value[2] <= #161) and (value[2] >= #254) then Result := false else Result := true end else Result := true; end;function GBtoBIG5(value: string): string; var leng, idx : integer; tmpStr : string[2]; Offset : integer; output : string; begin output := ''; leng := length(value); idx := 1; while idx <= leng do begin tmpStr := value[idx]+ value[idx + 1]; if isGB(tmpStr) then begin offset:=GBOffset(tmpStr); if (offset >= 0) and (offset <= 8177) then begin output := output + WordToString(GBOrder[offset]); inc(idx); end else output := output + value[idx] ; end else output := output + value[idx] ; inc(idx, 1); end; Result := output; end;function BIG5toGB(value: string): string; var leng, idx : integer; tmpStr : string[2]; output : string; offset : integer; begin output := ''; leng := length(value); idx := 1; while idx <= leng do begin tmpStr := value[idx]+ value[idx + 1]; if isBIG5(tmpStr) then begin offset:=BIG5Offset(tmpStr); if (offset >= 0) and (offset <= 14757) then begin output := output + WordToString(BIG5Order[offset]); inc(idx); end else output := output + value[idx]; end else output := output + value[idx]; inc(idx); end; Result := output; end;initialization BIG5Order[0] := $2020; BIG5Order[1] := $A3AC; BIG5Order[2] := $A1A2; BIG5Order[3] := $A1A3; BIG5Order[4] := $2020; BIG5Order[5] := $A1A4; .. .. .. .. BIG5Order[14753] := $2020; BIG5Order[14754] := $2020; BIG5Order[14755] := $2020; BIG5Order[14756] := $2020; BIG5Order[14757] := $2020;
SysUtils, Windows;function StrConv(pszSrc : PChar; cpSrc : UINT; pszDest : PChar; cbDest :
Longint; cpDest : UINT) : Longint;
var
buf : PWideChar;
cwLen : LongInt;
begin
cwLen := MultiByteToWideChar(cpSrc,
0,
pszSrc,
-1,
nil,
0);
GetMem(buf, (cwLen+1) * SizeOf(WideChar));
ZeroMemory(buf, (cwLen+1) * SizeOf(WideChar));
MultiByteToWideChar(cpSrc,
0,
pszSrc,
StrLen(pszSrc),
buf,
cwLen+1);
Result := WideCharToMultiByte(cpDest,
0,
buf,
-1,
pszDest,
cbDest,
nil,
nil);
FreeMem(buf);
end;function StringConv(const Src : string; cpSrc, cpDest : UINT) : string;
var
buf : PChar;
len : Longint;
begin
len := StrConv(PChar(Src), cpSrc, nil, 0, cpDest);
GetMem(buf, len);
ZeroMemory(buf, len);
StrConv(PChar(Src), cpSrc, buf, len, cpDest);
Result := string(buf);
FreeMem(buf);
end;function CHTStringToCHSString(Src : string) : string;
var
buf : PChar;
len : Integer;
begin
len := LCMapString($804, // LCID_CHINESE_SIMPLIFIED
LCMAP_SIMPLIFIED_CHINESE,
PChar(Src),
-1,
nil,
0);
GetMem(buf, len);
ZeroMemory(buf, len);
LCMapString($804, // LCID_CHINESE_SIMPLIFIED
LCMAP_SIMPLIFIED_CHINESE,
PChar(Src),
-1,
buf,
len);
Result := string(buf);
FreeMem(buf);
end;function CHSStringToCHTString(Src : string) : string;
var
buf : PChar;
len : Integer;
begin
len := LCMapString($404, // LCID_CHINESE_SIMPLIFIED
LCMAP_TRADITIONAL_CHINESE,
PChar(Src),
-1,
nil,
0);
GetMem(buf, len);
ZeroMemory(buf, len);
LCMapString($404, // LCID_CHINESE_SIMPLIFIED
LCMAP_TRADITIONAL_CHINESE,
PChar(Src),
-1,
buf,
len);
Result := string(buf);
FreeMem(buf);
end;function CHTToCHS(pszSrc, pszDest : PChar; cbDest : Longint) : Longint;
begin
Result := LCMapString($804, // LCID_CHINESE_SIMPLIFIED
LCMAP_SIMPLIFIED_CHINESE,
pszSrc,
-1,
pszDest,
cbDest);
end;function CHSToCHT(pszSrc, pszDest : PChar; cbDest : Longint) : Longint;
begin
Result := LCMapString($404, // LCID_CHINESE_SIMPLIFIED
LCMAP_TRADITIONAL_CHINESE,
pszSrc,
-1,
pszDest,
cbDest);
end;function Big5StringToGBString(Src : string) : string;
begin
Result := CHTStringToCHSString(StringConv(Src, 950, 936));
end;function Big5ToGB(pszSrc, pszDest : PChar; cbDest : Longint) : Longint;
var
buf : PChar;
len : Integer;
begin
len := StrLen(pszSrc)+1;
GetMem(buf, len*2);
ZeroMemory(buf, len*2);
StrConv(pszSrc, 950, buf, 936, len*2);
Result := CHTToCHS(buf, pszDest, cbDest);
FreeMem(buf);
end;function GBStringToBig5String(Src : string) : string;
begin
Result := CHSStringToCHTString(StringConv(Src, 936, 950));
end;function GBToBig5(pszSrc, pszDest : PChar; cbDest : Longint) : Longint;
var
buf : PChar;
len : Integer;
begin
len := StrLen(pszSrc)+1;
GetMem(buf, len*2);
ZeroMemory(buf, len*2);
StrConv(pszSrc, 936, buf, 950, len*2);
Result := CHSToCHT(buf, pszDest, cbDest);
FreeMem(buf);
end;
function GBtoBIG5(value: string): string;
function BIG5toGB(value: string): string;implementationvar
BIG5Order: array[0..14757] of Word;
GBOrder : array[0..8177] of Word;function GBOffset(value: string): integer;
begin
if length(value) >= 2 then
Result := (Ord(value[1]) - 161) * 94 + (Ord(value[2]) - 161)
else
Result := -1;
end;function BIG5Offset(value: string): integer;
begin
Result := -1;
if length(value) >= 2 then
begin
if (Ord(value[2]) >= 64) and (Ord(value[2]) <= 126) then
Result := (Ord(value[1]) - 161) * 157 + (Ord(value[2]) - 64);
if (Ord(value[2]) >= 161) and (Ord(value[2]) <= 254) then
Result := (Ord(value[1]) - 161) * 157 + 63 + (Ord(value[2]) - 161);
end
end;function WordToString(value: Word): string;
begin
Result := Chr(Hi(value)) + Chr(Lo(Value));
end;function isBIG5(value: string): Boolean;
begin
if (length(value)>=2) then
begin
if (value[1] < #161) then
Result := false
else
if ((value[2] >= #64) and (value[2] <= #126)) or ((value[2] >= #161) and (value[2] <= #254)) then
Result := true
else
Result := false
end
else
Result := false
end;function isGB(value: string): Boolean;
begin
if (length(value)>=2) then
begin
if (value[1] <= #161) and (value[1] >= #247) then
Result := false
else
if (value[2] <= #161) and (value[2] >= #254) then
Result := false
else
Result := true
end
else
Result := true;
end;function GBtoBIG5(value: string): string;
var
leng, idx : integer;
tmpStr : string[2];
Offset : integer;
output : string;
begin
output := '';
leng := length(value);
idx := 1;
while idx <= leng do
begin
tmpStr := value[idx]+ value[idx + 1];
if isGB(tmpStr) then
begin
offset:=GBOffset(tmpStr);
if (offset >= 0) and (offset <= 8177) then
begin
output := output + WordToString(GBOrder[offset]);
inc(idx);
end
else
output := output + value[idx] ;
end
else
output := output + value[idx] ; inc(idx, 1);
end;
Result := output;
end;function BIG5toGB(value: string): string;
var
leng, idx : integer;
tmpStr : string[2];
output : string;
offset : integer;
begin
output := '';
leng := length(value);
idx := 1;
while idx <= leng do
begin
tmpStr := value[idx]+ value[idx + 1];
if isBIG5(tmpStr) then
begin
offset:=BIG5Offset(tmpStr);
if (offset >= 0) and (offset <= 14757) then
begin
output := output + WordToString(BIG5Order[offset]);
inc(idx);
end
else
output := output + value[idx];
end
else
output := output + value[idx]; inc(idx);
end;
Result := output;
end;initialization BIG5Order[0] := $2020;
BIG5Order[1] := $A3AC;
BIG5Order[2] := $A1A2;
BIG5Order[3] := $A1A3;
BIG5Order[4] := $2020;
BIG5Order[5] := $A1A4;
..
..
..
..
BIG5Order[14753] := $2020;
BIG5Order[14754] := $2020;
BIG5Order[14755] := $2020;
BIG5Order[14756] := $2020;
BIG5Order[14757] := $2020;
GBOrder[0] := $2020;
GBOrder[1] := $A142;
GBOrder[2] := $A143;
GBOrder[3] := $A145;
GBOrder[4] := $A1C2;
GBOrder[5] := $A3BE;
..
..
..
GBOrder[8175] := $ECB8;
GBOrder[8176] := $C24D;
GBOrder[8177] := $2020;
GBOrder[8177] := $2020;
end.