Link to home
Start Free TrialLog in
Avatar of coldboy
coldboy

asked on

I cannot understand why there is Error!

I create a program to encode/decode an string.
You may understand it by the code below.
The problem is I cannot understand why it generately such serious error!
-------------Unit1.pas----------------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, XPMan;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    Label3: TLabel;
    Edit3: TEdit;
    Button3: TButton;
    Label4: TLabel;
    Edit4: TEdit;
    Button4: TButton;
    XPManifest1: TXPManifest;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

type
  TCodeRange=Byte;
  TCodeArr=array of TCodeRange;
var
  Form1: TForm1;
  PCode2Char: Array[TCodeRange] {0-9A-Z} of char;
  Char2PCode: Array[char] of TCodeRange;
  PCode2NCode: Array[TCodeRange] of TCodeRange;
  NCode2PCode: Array[TCodeRange] of TCodeRange;
implementation
uses USysPrepare;
{$R *.dfm}
  procedure BuiltCodeTable;
  var c1: integer;
  begin
    for c1:=0 to 36 do begin
      if c1<=9 then begin
        PCode2Char[c1]:= chr(ord('0')+c1);
      end else if c1<36 then begin
        PCode2Char[c1]:= chr(ord('A')+c1-10);
      end else begin //c1=36
        PCode2Char[c1]:= '@';
      end;
      Char2PCode[PCode2Char[c1]]:=c1;
    end;
    for c1:=0 to 35 do begin
      PCode2NCode[c1]:= 35-c1;
      NCode2PCode[35-c1]:= c1;
    end;
  end;

  function String2PCodeArr(const st: string): TCodeArr;
  var c1: integer; _st: string;
  begin
    _st:= UpperCase(st);
    Setlength(Result, length(_st));
    for c1:= 1 to length(_st) do begin
      Result[c1-1]:= Char2PCode[_st[c1]];
    end;
  end;

  function CodeArr2String(CodeArr: TCodeArr): string;
  var c1: integer; n: integer;
  begin
    n:= length(CodeArr);
    Setlength(Result, n);
    for c1:= 0 to length(CodeArr)-1 do begin
      Result[c1+1]:=PCode2Char[CodeArr[c1]];
    end;
    //Result:='WOWOWOW';
  end;

  function ConvertPCodetoNCode(const PCodeArr: TCodeArr): TCodeArr;
  var c1: integer;
  begin
    setlength(Result, length(PCodeArr));
    For c1:= 0 to length(PCodeArr)-1 do
      Result[c1]:= PCode2NCode[PCodeArr[c1]];
  end;

  function ConvertNCodetoPCode(const NCodeArr: TCodeArr): TCodeArr;
  var c1: integer;
  begin
    setlength(Result, length(NCodeArr));
    for c1:= 0 to length(NCodeArr)-1 do
      Result[c1]:= NCode2PCode[NCodeArr[c1]];
  end;

type
  Tkey=record a1, b1, a2, b2: integer end;
const
  key_Code2BlockCode: Tkey =(a1: 23; b1: 11; a2: 9; b2: 12);
  key_BlockCode2Code: Tkey =(a1: 17; b1: 6; a2: 15; b2: 11);

  function BlockCode(CodeArr: TCodeArr; const key: Tkey): TCodeArr;
  var c1: integer; n: integer;
  begin
    n:= length(CodeArr);
    if n mod 2=1 then begin
      Setlength(CodeArr, n+1);
      CodeArr[n]:=0;
    end;
    setlength(Result, length(CodeArr));
    for c1:= 0 to length(CodeArr) div 2 do begin
      Result[c1*2]:= (key.a1*CodeArr[c1*2]+key.b1*CodeArr[c1*2+1]) mod 37;
      Result[c1*2+1]:= (key.a2*CodeArr[c1*2]+key.b2*CodeArr[c1*2+1]) mod 37;
    end;
  end;

  function Code2Blockcode(const CodeArr: TCodeArr): TCodeArr;
  begin
    Result:= BlockCode(CodeArr, key_Code2BlockCode);
  end;

  function Blockcode2Code(const CodeArr: TCodeArr): TCodeArr;
  begin
    Result:= BlockCode(CodeArr, key_BlockCode2Code);
  end;

procedure TForm1.Button1Click(Sender: TObject);
var st: string;
begin
  st:= Edit1.Text;
  Edit2.Text:= CodeArr2String(ConvertPCodetoNCode(String2PCodeArr(st)));
end;

procedure TForm1.Button2Click(Sender: TObject);
var st: string;
begin
  st:= Edit2.Text;
  Edit3.Text:= CodeArr2String(Code2Blockcode(String2PCodeArr(st)));
  //Edit3.Text:= CodeArr2String(String2PCodeArr(Edit1.Text));
end;

procedure TForm1.Button3Click(Sender: TObject);
var st: string;
begin
  st:= Edit3.Text;
  Edit4.Text:= CodeArr2String(BlockCode2Code(String2PCodeArr(st)));
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  Edit1.Text:= CodeArr2String(ConvertNCodetoPCode(String2PCodeArr(Edit4.Text)));
end;

initialization
  BuiltCodeTable;
finalization
end.
--------------------Unit1.dfm------------------------
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 433
  Height = 196
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Courier'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 24
    Top = 13
    Width = 32
    Height = 13
    Caption = 'Code'
  end
  object Label2: TLabel
    Left = 24
    Top = 45
    Width = 40
    Height = 13
    Caption = 'Code1'
  end
  object Label3: TLabel
    Left = 24
    Top = 76
    Width = 40
    Height = 13
    Caption = 'Code2'
  end
  object Label4: TLabel
    Left = 24
    Top = 108
    Width = 40
    Height = 13
    Caption = 'Code2'
  end
  object Edit1: TEdit
    Left = 80
    Top = 8
    Width = 233
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
  end
  object Edit2: TEdit
    Left = 80
    Top = 40
    Width = 233
    Height = 21
    TabOrder = 1
    Text = 'Edit2'
  end
  object Button1: TButton
    Left = 320
    Top = 8
    Width = 75
    Height = 21
    Caption = 'Encode1'
    TabOrder = 2
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 320
    Top = 40
    Width = 75
    Height = 21
    Caption = 'Encode2'
    TabOrder = 3
    OnClick = Button2Click
  end
  object Edit3: TEdit
    Left = 80
    Top = 71
    Width = 233
    Height = 21
    TabOrder = 4
    Text = 'Edit3'
  end
  object Button3: TButton
    Left = 320
    Top = 71
    Width = 75
    Height = 21
    Caption = 'Encode3'
    TabOrder = 5
    OnClick = Button3Click
  end
  object Edit4: TEdit
    Left = 80
    Top = 103
    Width = 233
    Height = 21
    TabOrder = 6
    Text = 'Edit4'
  end
  object Button4: TButton
    Left = 320
    Top = 103
    Width = 75
    Height = 21
    Caption = 'Encode4'
    TabOrder = 7
    OnClick = Button4Click
  end
  object XPManifest1: TXPManifest
    Left = 88
    Top = 128
  end
end
------------------------------------------------
The Error will appear when you change Edit2.Text to some value (like 'Edit2aaaaaaaa') and click Encode2
(Much case, change the value of Edit2.Text and you will see)
Please show me why there is a error.
I uses Delphi7
Avatar of RickJ
RickJ

What is the error that you receive??
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coldboy

ASKER

A error generated by a function and by raised by another function so I have unable to find it. Thank PierreC for finding my error! Your answer accepted with grade A!
Glad to help...