给你一个单元
unit UnitGb2Big;
interface
uses
Sysutils,ComCtrls;
function GBToBIG5(GBSource: string;ProgressBar:TProgressBar=NIL): string;
function BIG5ToGB(BIG5Source: string;ProgressBar:TProgressBar=NIL): string;
function ReadTable(const AFileName: string): boolean;
function SaveTable(const AFileName: string): boolean;
implementation
var
OrderInited: boolean=False;
BIG5Order: array[0..14757] of Word;
GBOrder : array[0..8177] of Word;
function GBOffset(GBChar: string): integer;
begin
Result:=(Ord(GBChar[1])-161)*94+(Ord(GBChar[2])-161); //(hi-161)*94+(lo-161)
end;
function BIG5Offset(BIG5Char: string): integer;
begin
if Ord(BIG5Char[2]) in [64..126] then
Result:=(Ord(BIG5Char[1])-161)*157+(Ord(BIG5Char[2])-64) //(hi-161)*157+(lo-64)
else //if Ord(BIG5Char[2] in [161..254] then
Result:=(Ord(BIG5Char[1])-161)*157+Ord(BIG5Char[2])-98;//(hi-161)*157+63+(lo-161)
end;
function WordToString(Value: Word): string;
begin
Result:=Chr(Hi(Value))+Chr(Lo(Value));
end;
function IsBIG5(BIG5Char: string): Boolean;
begin
//BIG5码高八位为A1-FE,低八位为40-FE(7E-A1为空白)
Result:=False;
if Length(BIG5Char)>=2 then
if BIG5Char[1]>=#161 then
Result:=(BIG5Char[2] in [#64..#126]) or (BIG5Char[2] in [#161..#254]);
end;
function IsGB(GBChar: string): Boolean;
begin
Result:=False;
if Length(GBChar)>=2 then
Result:=(GBChar[1] in [#161..#247]) and (GBChar[2] in [#161..#255]);
end;
function GBToBIG5(GBSource: string;ProgressBar:TProgressBar=NIL): string;
var
Len, I : integer;
TmpStr : string[2];
Offset : integer;
begin
Result:='';
Len:=Length(GBSource);
if ProgressBar<>NIL then
ProgressBar.Max:=Len;
I:=1;
while I<=Len do
begin
TmpStr:=GBSource+GBSource[I+1];
if IsGB(TmpStr) then
begin
Offset:=GBOffset(TmpStr);
if (Offset>=0) and (Offset<=8177) then
begin
Result:=Result+WordToString(GBOrder[Offset]);
Inc(I);
end
else
Result:=Result+GBSource;
end
else
Result:=Result+GBSource;
Inc(I);
if ProgressBar<>NIL then
if (I Mod LARGESTEP)=0 then
ProgressBar.Position:=I;
end;
end;
function BIG5toGB(BIG5Source: string;ProgressBar:TProgressBar=NIL): string;
var
Len,I : integer;
TmpStr : string[2];
Offset : integer;
begin
Result:='';
Len:=Length(BIG5Source);
if ProgressBar<>NIL then
ProgressBar.Max:=Len;
I:=1;
while I<=Len do
begin
TmpStr:=BIG5Source+BIG5Source[I+1];
if IsBIG5(TmpStr) then
begin
Offset:=BIG5Offset(TmpStr);
if (Offset>=0) and (Offset<=14757) then
begin
Result:=Result+WordToString(BIG5Order[Offset]);
Inc(I);
end
else
Result:=Result+BIG5Source;
end
else
Result:=Result+BIG5Source;
Inc(I);
if ProgressBar<>NIL then
if (I Mod LARGESTEP)=0 then
ProgressBar.Position:=I;
end;
end;
function ReadTable(const AFileName: string): boolean;
var
F:TextFile;
ChH:Char;
ChL:Char;
I:integer;
begin
if Not FileExists(AFileName) then
begin
MsgBox('无法打开码表文件'+AFileName+','+
NEWLINE+'请确认该文件与本程序在同一目录下。');
Result:=False;
exit;
end;
if OrderInited then
begin
Result:=True;
exit;
end;
AssignFile(F,AFilename);
Reset(F);
for I:=0 to High(BIG5Order) do
begin
Read(F, ChH, ChL);
BIG5Order:=(Ord(ChH) Shl 8)+Ord(ChL);
end;
for I:=0 to High(GbOrder) do
begin
Read(F, ChH, ChL);
GbOrder:=(Ord(ChH) Shl 8)+Ord(ChL);
end;
CloseFile(F);
OrderInited:=True;
Result:=True;
end;
function SaveTable(const AFileName: string): boolean;
var
F:TextFile;
I:integer;
begin
AssignFile(F,AFilename);
ReWrite(F);
for I:=0 to High(BIG5Order) do
Write(F,Char(Hi(BIG5Order)),Char(Lo(BIG5Order)));
for I:=0 to High(GBOrder) do
Write(F,Char(Hi(GBOrder)),Char(Lo(GBOrder)));
CloseFile(F);
Result:=True;
end;
end.
码表文件发到你的邮箱了