SRL Archive - OSR Intermediate Scripting Tutorials

Ord.


Naum

2009-06-20 15:48:05

Ord

I have seen 'ord' used a few times mainly in misc/smart.scar. So I thought I'd post something from delphi basics:

What is ord?
The 'Ord' function returns an integer value for any ordinal type.
It is mainly used to convert characters into their 'numeric equivalents'
N.B Instead of using Ord() you can also use the hash key '#' and the respective key code (see the SCAR manual).

How is it used?
Ord(Arg : Variant) : Int64; //Weirdly it does not show up in the SCAR menu??

Example:

var
A : Char;
C : Char;
W : WideChar;
E : Boolean;
I : Integer;
I64 : Int64;

begin
// Set the ordinal type values
A := ''''; //Place it comes in on the anscii table
C := 'C'; //Place it comes in on the anscii table
W := 'W'; //Place it comes in on the anscii table
E := True; //False = 0, True = 1.
I := 22;
I64 := 64;

// And show the value of each
WriteLn('A = '+IntToStr(Ord(A)));
WriteLn('C = '+IntToStr(Ord(C)));
WriteLn('W = '+IntToStr(Ord(W)));
WriteLn('E = '+IntToStr(Ord(E)));
WriteLn('I = '+IntToStr(Ord(I)));
WriteLn('I64 = '+IntToStr(Ord(I64)));
end.

To help you also look at the ASCII table in the SCAR manual.

If you have difficulty with understanding these types try reading here

Chr

Definition:
The Chr function converts an IntValue integer into either an AnsiChar or WideChar as appropriate.

My definition: Does the opposite of ord, converts the integer value of a char back into it.

How is it used?
Chr(IntValue : Integer) : String;

Example:
var
tab : char;
crlf : string;
begin
// Show the use of Chr
tab := Chr(9);
crlf := Chr(13)+Chr(10);
WriteLn('Hello'+tab+'World');
WriteLn('');
WriteLn('Hello'+crlf+'World');
WriteLn('');

// Show the equivalent use of ^
tab := 'I'; // I = 9th capital of the alphabet
crlf := 'MJ'; // M = 13th, J = 10th letters
WriteLn('Hello'+tab+'World');
WriteLn('');
WriteLn('Hello'+crlf+'World');
end;

Wizzup?

2009-06-20 16:19:33

As a FYI, the 'Char Table' in SCAR is just ASCII.

Naum

2009-06-20 18:50:37

From: Wizzup?
As a FYI, the 'Char Table' in SCAR is just ASCII.


Thanks for that wizzup :)

Shuttleu

2009-06-20 19:06:21

and to get the numerical value back into a char you would just do chr(int) ?

~shut

Naum

2009-06-20 19:44:22

From: Shuttleu
and to get the numerical value back into a char you would just do chr(int) ?

~shut


Exactly, Just added that :)

The Val procedure can also convert into an Extended or an integer.

Da 0wner

2009-06-21 02:22:27


program New;

begin
Writeln(chr(ord('w')));
end.

:D

Useful, however I already knew about this :p.

ian.

2009-06-21 02:43:57

You can also just do #13 instead of Chr(13); ;]

Da 0wner

2009-06-21 02:47:02

Not in the way I used it…

ian.

2009-06-21 02:50:41

I was telling Naum something he could add to the tut :p Yours is perfect, dear. :]

Da 0wner

2009-06-21 02:55:13

Oh, sorry :o. Yeah you can just use an octophorpe and the number code right after it.

ian.

2009-06-21 02:56:32

Thanks for restating exactly what I just said.. >.> haha ^^

Da 0wner

2009-06-21 03:15:55

You're welcome (h) xD.

Macro_FTW

2009-06-21 03:54:12

Great tutorial. Should be helpful for those who want to convert char's and ints alike. :D

~Macro_FTW

Naum

2009-06-22 20:20:46

Thanks, added parts to the tutorial :)

Macro_FTW

2009-06-28 03:03:08

Note: Using the # only allows the use of literal constants (numbers typed in)

Constants, such as

const
v = 9;
don't work, nor do variables.