Convert CHAR to INTEGER

Topics related to the use of Oberon language features
Post Reply
Helpdesk
Posts: 34
Joined: Sat Jan 01, 2011 5:43 am
Contact:

Convert CHAR to INTEGER

Post by Helpdesk » Tue Feb 01, 2011 11:03 am

How do I convert a CHAR (from serial input) to an INTEGER?

cfbsoftware
Site Admin
Posts: 496
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Convert CHAR to INTEGER

Post by cfbsoftware » Tue Feb 01, 2011 11:10 am

The built-in ORD function will typecast a CHAR to INTEGER i.e.

Code: Select all

VAR
  ch: CHAR;
  i: INTEGER;
...
...
  i := ORD(ch);
This gives you the corresponding numeric ASCII value for that character. Knowing this, if you want to convert a single character (e.g. '9') to the integer value (e.g. 9) you can then say:

Code: Select all

IF ('0' <= ch) & (ch <= '9') THEN i := ORD(ch) - ORD('0') END;
Note that the Convert library procedure StrToInt can be used to convert strings to
integers.

Post Reply