Page 1 of 1

Convert CHAR to INTEGER

Posted: Tue Feb 01, 2011 11:03 am
by Helpdesk
How do I convert a CHAR (from serial input) to an INTEGER?

Re: Convert CHAR to INTEGER

Posted: Tue Feb 01, 2011 11:10 am
by cfbsoftware
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.