CVCode.pas


本文整理自网络,侵删。

 

// GB2312/BIG5 convert unit for Delphi 3
// ver 1.01
// By Tom Lee  1997/9/5
// E-Mail Address : Tomm.bbs@csie.nctu.edu.tw
// It is Freeware !

unit CVCode;

interface

uses
  sysutils, Forms, IniFiles;

 function GBtoBIG5(value: string): string;
 function BIG5toGB(value: string): string;
 function GetGBOrder(value: string): string;
 function GetBIG5Order(value: string): string;
 function WriteGBOrder(value, aBIG5: string): Boolean;
 function WriteBIG5Order(value, aGBK: string): Boolean;

implementation

function GetGBOrder(value: string): string;
var
  i, j: LongInt;
begin
  for i := $A1 to $A9 do
  for j := $A1 to $FE do
  begin
    if (chr(i) + chr(j))= value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;

  for i := $B0 to $F7 do
  for j := $A1 to $FE do
  begin
    if (chr(i) + chr(j))= value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;

  for i := $81 to $A0 do
  for j := $40 to $FE do
  begin
    if (chr(i) + chr(j))= value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;

  for i := $AA to $FE do
  for j := $40 to $A0 do
  begin
    if (chr(i) + chr(j))= value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;

  for i := $A8 to $A9 do
  for j := $40 to $A0 do
  begin
    if (chr(i) + chr(j))= value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;
 
end;

function GetBIG5Order(value: string): string;
var
  i, j: LongInt;
begin
  for i := $A1 to $F9 do
  for j := $40 to $FE do
  begin
    if (chr(i) + chr(j)) = value then
    begin
      Result := inttohex(i, 2) + inttohex(j, 2);
      break;
    end;
  end;
end;

function GBOffset(value: string): integer;
begin
  if length(value) >= 2 then
    Result := (Ord(value[1]) - 161) * 94 + (Ord(value[2]) - 161)
//    Result := (Ord(value[1]) - 129) * 190 + (Ord(value[2]) - 64) - Ord(Value[2]) div 128
  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
//    if (value[1] <= #129) and (value[1] >= #254) then
      Result := false
    else
      if (value[2] <= #64) 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;
  IniFile        : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  try
  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 <= 22704) then
      begin
        output := output + WordToString(IniFile.ReadInteger('GB', 'GBOrder[' + inttostr(offset) + ']', 0));
        inc(idx);
      end
      else
        output := output + value[idx] ;
    end
    else
      output := output + value[idx] ;

    inc(idx, 1);
  end;
  Result := output;
  finally
    IniFile.Free;
  end;
end;

function BIG5toGB(value: string): string;
var
  leng, idx      : integer;
  tmpStr         : string[2];
  output         : string;
  offset         : integer;
  IniFile        : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  try
  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 <= 13972) then
      begin
        output := output + WordToString(IniFile.ReadInteger('BIG5', 'BIG5Order[' + inttostr(offset) + ']', 0));
        inc(idx);
      end
      else
        output := output + value[idx];
    end
    else
      output := output + value[idx];

    inc(idx);
  end;
  Result := output;
  finally
    IniFile.Free;
  end;
end;

function WriteGBOrder(value, aBIG5: string): Boolean;
var
  tmpStr         : string[2];
  Offset         : integer;
  IniFile        : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  Result := false;
  try
    tmpStr := chr(strtoint('$' + copy(value, 1, 2))) + chr(strtoint('$' + copy(value, 3, 2)));
    if isGB(tmpStr) then
    begin
      offset:=GBOffset(tmpStr);
      if (offset >= 0) and (offset <= 22704) then
      begin
        IniFile.WriteString('GB', 'GBOrder[' + inttostr(offset) + ']', '$' + aBIG5);
        Result := true;
      end
      else begin
        Application.MessageBox('编码顺序有小于0,不能存档.', PChar(Application.Title), 0);
      end;
    end;
  finally
    IniFile.Free;
  end;
end;

function WriteBIG5Order(value, aGBK: string): Boolean;
var
  tmpStr         : string[2];
  Offset         : integer;
  IniFile        : TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Language');
  Result := false;
  try
    tmpStr := chr(strtoint('$' + copy(value, 1, 2))) + chr(strtoint('$' + copy(value, 3, 2)));
    if isBIG5(tmpStr) then
    begin
      offset:=BIG5Offset(tmpStr);
      if (offset >= 0) and (offset <= 13972) then
      begin
        IniFile.WriteString('BIG5', 'BIG5Order[' + inttostr(offset) + ']', '$' + aGBK);
        Result := true;
      end
      else begin
        Application.MessageBox('编码顺序有小于0,不能存档.', PChar(Application.Title), 0);
      end;
    end;
  finally
    IniFile.Free;
  end;
end;

end.

相关阅读 >>

Delphi获取注册表run下所有值

Delphi 安卓原生的toast温柔提示

Delphi shellexecute openurl 的跨平台实现

idhttp使用时内存猛增,如何解决

Delphi xe5 android 使用system.zip单元释放资源文件

Delphi通过adoquery控件实现sqlserver数据库多结果集的数据打印

Delphi memo1 统计行 列

Delphi 获取文件的最新修改时间

Delphi listview导出到excel

Delphi 保证当前程序显示在最前

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...