Results 1 to 15 of 15

Thread: Ord.

  1. #1
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default Ord and Chr.

    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:

    SCAR Code:
    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:
    SCAR Code:
    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;
    Last edited by Naum; 06-23-2009 at 12:22 AM.

  2. #2
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,691
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

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



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  3. #3
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by Wizzup? View Post
    As a FYI, the 'Char Table' in SCAR is just ASCII.
    Thanks for that wizzup

  4. #4
    Join Date
    Aug 2007
    Location
    in a random little world
    Posts
    5,778
    Mentioned
    0 Post(s)
    Quoted
    7 Post(s)

  5. #5
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Quote Originally Posted by Shuttleu View Post
    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.
    Last edited by Naum; 06-20-2009 at 11:56 PM.

  6. #6
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    SCAR Code:
    program New;

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



    Useful, however I already knew about this .

  7. #7
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  8. #8
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Not in the way I used it...

  9. #9
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  10. #10
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  11. #11
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  12. #12
    Join Date
    Jan 2008
    Location
    California, US
    Posts
    2,765
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    You're welcome (h) xD.

  13. #13
    Join Date
    Jan 2007
    Location
    Nomming bits in your RAM
    Posts
    385
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

    ~Macro_FTW
    Current Project: Catching up. XD. Potentially back for the summer, depending on how things go.

  14. #14
    Join Date
    Mar 2007
    Posts
    4,810
    Mentioned
    3 Post(s)
    Quoted
    3 Post(s)

    Default

    Thanks, added parts to the tutorial

  15. #15
    Join Date
    Jan 2007
    Location
    Nomming bits in your RAM
    Posts
    385
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

    Constants, such as
    SCAR Code:
    const
      v = 9;
    don't work, nor do variables.
    Current Project: Catching up. XD. Potentially back for the summer, depending on how things go.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •