Using the ICHAR and CHAR Intrinsic Functions

ID: Q51495


The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.1, 5.0, 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0, 1.0a
  • Microsoft Fortran Powerstation 32 for Windows NT, versions 1.0, 4.0
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, 5.1


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 query words: kbinf 1.00 4.10 5.00 5.10 type conversion

Keywords : kbcode kbFortranPS kbLangFortran
Version : :1.0,1.0a,4.0,4.1,5.0,5.1
Platform : MS-DOS NT OS/2 WINDOWS
Issue type :


Last Reviewed: November 1, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.