HOME - - / - - / - - LAZARUS/ DELPHI COURSE INDEX - - / - - / - - TUTORIALS INDEX - - / - - / - - Other material for programmers

Lazarus and Delphi Course: Manipulating data in string type variables.

Page URL: StrCode1.htm

This page is information rich, and a has search button at the bottom of the page.

Please don't dismiss it because it isn't full of graphics, scripts, cookies, etc!

You will probably find the text easier to read if you make your browser window much narrower than usual. You may also want to change your browser's zoom level, to enlarge the text. Opera (at least) lets you change zoom level easily. The text will adapt nicely to the settings you decide give the best results for your needs!

The lines of sample Lazarus/ Delphi code in these pages will not "wrap". I.e., if a line is too long to show in the width you have set your browser too, parts of the line will be "off the page". Those lines will still copy/paste properly, at least in Opera. Please feel free to send feedback on the choices I've made! (Will you forgive me for not forcing upon you a column of links on the left and a column of ads on the right?)

This is just one exercise in a series of Lazarus / Delphi exercises. You will probably be best served by doing them in sequence... each assumes some prior knowledge. Material © TK Boyd, sheepdogsoftware.co.uk, 4/05-6/20.

You may find typos and rough edges in this. None-the-less, the basic information should be accurate. If something seems wrong, or if you find I've assumed knowledge without explaining it in a previous lesson, please let me know. Please forgive matters of typos, etc. for now. I am not inherently sloppy! The blemishes will be dealt with later.

In this lesson,.
--- You create an application which encodes text.

You should learn about...
--- Manipulating data which is in a string type variable

Lazarus/ Delphi built in functions:
--- Ord
--- Chr
--- Length (of a string)
In this lesson, you will create an application which will encrypt short messages.

Start a new application. Rename the form strcode1f1. Save the unit as strcode1u1. Save the project as strcode1.

Put an edit box and a label on the form. Rename the edit box ePlainText. Rename the label laEncrypted. Make the edit box's OnChange handler...

Initially, our "code" is going to be very simple. If the plaintext is "ABC", then the code version is going to be "BCD".

Before we can get very far, we have to realise that Delphi doen't "know" the alphabet the way we do.

We also have to know that most modern computer languages agree that 65 is the number of the letter "A", 66 is the numbr for "B", 67 the number for "C", etc. Also, the lower case characters have different numbers. "a" is 97, "b" is 98, etc.

Happily, Delphi gives us ways to go back and forth from character to numbr or vice versa.

Make the form's OnCreate event handler....
procedure Tstrcode1f1.FormCreate(Sender: TObject);
When you run the program, you'll see the label caption change to "A".

Now make the form's OnCreate handler be...
procedure Tstrcode1f1.FormCreate(Sender: TObject);
Save project. Try to guess what you'll see! Run the application. Now you should see.....

-- --- v v v v

-- --- v v v v

-- --- v v v v

... Hello World where laEncryped was.

I hope you remember from earlier lessons that we talked about "data types"? The dat we're working here is string type data. That's the only sort of data you can put into a test or a caption property.

To recap: String data was Good because it could consist of almost anything. It was Limited in that you couldn't do many things with it, including mathematical things. So what's this "+" that we see in the OnCreate handler above? You should remember that, when applied to strings, the plus sign merely means "stick these two things together". (Proper term: concatenate.)

Something new: I haven't before made much fuss over the idea of a "character". Characters are the things strings are made of... e.g. a,b,c... 1,2,3,... punctuation marks, spaces, etc.

If a string is only one character long, it is still a string. It is a string even when it is NO characters "long", which we call an empty string.

Now another new thing. You've seen it in action, but I've not made a fuss about it. Many, many times in programming, you will use functions. "chr(65)", which we used at the start of this lesson is a function. More correctly, "chr()" is a function, and you must supply a paramter to the function, between the brackets, and, in the case of chr, that parameter must be a number.

The function "chr(65)" boils down to the character "A". I hope you remember me speaking of "boiling down" before... it is important.

There's another function which we will use in this lesson: ord(). To take an example, ord('A') "boils down" to the number 65. Chr takes us from numbers to characters. Ord takes us from characters to numbers. It is all simple enough... it is quite easy to get in a muddle. For instance if you say "ord(65)", Delphi will not be happy: It expects a character with ord, not a number. Equally, if you did...
...Delphi would again be unhappy. It would talk of "incompatible types". this is because a caption requires a string, even if the string is only one character long, but ord "boils down to" a number, not a character.

Anyway... enough theory for the moment. Back to our encrypting program....

Make ePlainText's OnChange handler....
procedure Tstrcode1f1.ePlainTextChange(Sender: TObject);
... and run the application. As soon as you type anything into ePlainText, a "number" (actually a string, but one that looks like a number) appears on the label, telling us how long the string in ePlainText is. "Length()" is another function.

When you try to understand something like...
... start at the innermost level, "ePlain.text" in this case, and work outward.

ePlain.text holds whatever we've typed into the edit box.

length(ePlainText.text) is a number, the length of whatever we've typed into the edit box.

IntToStr(length(ePlainText.text)) is a string, even thought it will be something like "12". It is the string representation of the number which was the length of whatever we've typed into the edit box.

Tedious when you write it all out, try to say it. Fairly usable when you don't think about it too much. But you do need to know how to think about iot for those times when things are not working as you want them to. The computer never makes mistakes... it always does what you've told it to do... even when you've told it to do something other than what you wanted it to do!

Building on what we've seen so far, make ePlainText's OnChange handler....
procedure Tstrcode1f1.ePlainTextChange(Sender: TObject);
var iCount:integer;
for iCount:=1 to length(sToEncrypt) do begin
  end;// of for... begin...
This will make as laEncrypted show as many x's as there are characters... including spaces... in ePlaintext.

Next, change the...
... to ...
At this point, whatever you type in eToEncrypt should appear in laPlainText. Not a very good code! But progress towards our encrypter. The little bit of Delphi saying.....
... "boils down" to the character in the "iCount"th position of the string sToEncrypt. E.g. if sToEncrypt has become "ABCDEF", then sToEncrypt[3] "boils down" to "C". Note that, for these purposes, Delphi calls the 1st character character "1". Sometimes Delphi "counts from 0", i.e. the first character is called "character zero". But not here.

Remember "ord" and "chr", which we talked about earlier. See how they're going to help us?

... to ...
.... and you will have the application we set out to write. Enter "ABC", and you'll see the encrypted version is "BCD". What do you think Ifmmp!Xpsmne says??? (That's the encrypted version.)

If you're interested in codes, I've done much more on them in other places on the web. The material there isn't part of this, my Delphi Course, which is trying to be an ordered, sequenced attempt teach Delphi, but you may find it interesting.

I have no immediate plans to do more with codes in my Delphi Course, but if you want to suggest things which you think ought to be done, feel free!

   Search this site or the web        powered by FreeFind
  Site search Web search
Site Map    What's New    Search
Ad from page's editor: Yes.. I do enjoy compiling these things for you... I hope they are helpful. However.. they don't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, download something, and circulate it for me? Links on your page to this page would also be appreciated.
Click here to visit editor's Sheepdog Software freeware, shareware page.
Link to Tutorials main page Link to Lazarus/ Delphi Course index

To email this page's editor, Tom Boyd.... Editor's email address. Suggestions welcomed! Please cite "strcode1.htm".

Click for W3.org HTML validity test Page has been tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org. Mostly passes.

AND passes... Click to check CSS validity

One final suggestion: Be sure you know all you need to about spyware.

. . . . . P a g e . . . E n d s . . . . .