?
Solved

I cannot understand why there is Error!

Posted on 2005-05-11
4
Medium Priority
?
388 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:coldboy
  • 2
4 Comments
 
LVL 8

Expert Comment

by:RickJ
ID: 13983337
What is the error that you receive??
0
 
LVL 14

Accepted Solution

by:
Pierre Cornelius earned 200 total points
ID: 13983898
Change the following function as follows:

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;    ======>>>SHOULD THIS NOT BE: CodeArr[n+1]:=0
    end;
    setlength(Result, length(CodeArr));
    for c1:= 0 to (length(CodeArr)-1) div 2 do begin ======>>> CHANGE HERE
      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;


regards
Pierre
0
 
LVL 5

Author Comment

by:coldboy
ID: 14001225
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!
0
 
LVL 14

Expert Comment

by:Pierre Cornelius
ID: 14005025
Glad to help...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question