汉字与区位码(1) - 转换函数


先上转换函数:


unit Unit1;



interface



uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls;



type

  TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  end;



var

  Form1: TForm1;



implementation



{$R *.dfm}



{查汉字区位码}

function Str2GB(const s: AnsiString): string;

const

  G = 160;

begin

  Result := Format('%d%d', [Ord(s[1])-G, Ord(s[2])-G]);

end;



{通过区位码查汉字}

function GB2Str(const n: Word): string;

const

  G = 160;

begin

  Result := string(AnsiChar(n div 100 + G) + AnsiChar(n mod 100 + G));

end;





{测试}

procedure TForm1.Button1Click(Sender: TObject);

begin

  ShowMessage(GB2Str(4582)); {万}

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

  ShowMessage(Str2GB('万')); {4582}

end;



end.


 
   

获取区位码表: 准备个 Memo 接收(注意使用了上面的函数)

var

  i,j: Byte;

  s: string;

begin

  s := '';

  for i := 1 to 94 do

  begin

    for j := 1 to 94 do

      s := Format('%s %s', [s, GB2Str(i*100 + j)]);

    Memo1.Lines.Add(s);

    s := '';

  end;

end;


 
   

汉字与区位码(2) - 分析

你可能感兴趣的:(函数)