PDA

View Full Version : Ord.



Naum
06-20-2009, 07:48 PM
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 (http://www.villavu.com/forum/showthread.php?t=40661)

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?
06-20-2009, 08:19 PM
As a FYI, the 'Char Table' in SCAR is just ASCII (http://en.wikipedia.org/wiki/ASCII).

Naum
06-20-2009, 10:50 PM
As a FYI, the 'Char Table' in SCAR is just ASCII (http://en.wikipedia.org/wiki/ASCII).

Thanks for that wizzup :)

Shuttleu
06-20-2009, 11:06 PM
and to get the numerical value back into a char you would just do chr(int) ?

~shut

Naum
06-20-2009, 11:44 PM
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
06-21-2009, 06:22 AM
program New;

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

:D

Useful, however I already knew about this :p.

ian.
06-21-2009, 06:43 AM
You can also just do #13 instead of Chr(13); ;]

Da 0wner
06-21-2009, 06:47 AM
Not in the way I used it...

ian.
06-21-2009, 06:50 AM
I was telling Naum something he could add to the tut :p Yours is perfect, dear. :]

Da 0wner
06-21-2009, 06:55 AM
Oh, sorry :o. Yeah you can just use an octophorpe and the number code right after it.

ian.
06-21-2009, 06:56 AM
Thanks for restating exactly what I just said.. >.> haha ^^

Da 0wner
06-21-2009, 07:15 AM
You're welcome (h) xD.

Macro_FTW
06-21-2009, 07:54 AM
Great tutorial. Should be helpful for those who want to convert char's and ints alike. :D

~Macro_FTW

Naum
06-23-2009, 12:20 AM
Thanks, added parts to the tutorial :)

Macro_FTW
06-28-2009, 07:03 AM
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.