Using the ICHAR and CHAR Intrinsic Functions

Last reviewed: December 11, 1995
Article ID: Q51495
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS and OS/2, versions 4.1, 5.0, 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0 and 1.0a
  • Microsoft FORTRAN PowerStation 32 for Windows NT, version 1.0 and 4.0

SUMMARY

The ICHAR intrinsic function converts one character to its integer ASCII value. The CHAR intrinsic function converts an integer to its corresponding ASCII character value. Both the ICHAR and CHAR functions use decimal integer values and the Extended ASCII Character Set.

The ICHAR function does not directly convert a number stored as a character array to its integer representation.

MORE INFORMATION

The integer value in each case must be between 0 and 255. The functions use modulo arithmetic to convert a number larger than 255 to a value between 0 and 255, for example, 256 "wraps around" to 0, 511 to 255, and so on.

The character string in each case must be a single character CHARACTER*1. If the character string is longer than one, the ICHAR function converts only the first character of the value.

The following code example demonstrates using the CHAR and ICHAR functions.

Sample Code

C Compile options needed: None

      PROGRAM EXAMPLE

      INTEGER*2   INT1, INT2
      CHARACTER*1 CHAR1, CHAR2

      CHAR1 = 'A'
      INT1  = ICHAR(CHAR1)

      INT2  = 90
      CHAR2 = CHAR(INT2)

      WRITE (*, *) 'CHAR1 = ', CHAR1, ' AND INT1 = ', INT1
      WRITE (*, *) 'CHAR2 = ', CHAR2, ' AND INT2 = ', INT2

      END

The output of program example is as follows:

   CHAR1 = A AND INT1 =          65
   CHAR2 = Z AND INT2 =          90

The values 65 and 90 correspond to the decimal ASCII values for "A" and "Z" respectively.

For more information on the CHAR and ICHAR intrinsic functions, see pages 240-241 of the Microsoft FORTRAN "Reference" manual for versions 5.0 and 5.1 or pages 323 and 325 of the Microsoft FORTRAN "Language Reference" manual for version 4.1 or the FORTRAN PowerStation Language Help.


Additional reference words: kbinf 1.00 4.10 5.00 5.10 type conversion
KBCategory: kbprg kbcode
KBSubcategory: FORTLngIss


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 11, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.