1. 程式人生 > >漢字與區位碼(1) - 轉換函式

漢字與區位碼(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) - 分析