Using the ICHAR and CHAR Intrinsic Functions (51495)



The information in this article applies to:

  • Microsoft FORTRAN Compiler for MS-DOS 4.1
  • Microsoft FORTRAN Compiler for MS-DOS 5.0
  • Microsoft FORTRAN Compiler for MS-DOS 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS 1.0
  • Microsoft FORTRAN PowerStation for MS-DOS 1.0a
  • Microsoft Fortran Powerstation 32 for Windows NT 1.0
  • Microsoft Fortran Powerstation 32 for Windows NT 4.0
  • Microsoft FORTRAN compiler for OS/2 4.1
  • Microsoft FORTRAN compiler for OS/2 5.0
  • Microsoft FORTRAN compiler for OS/2 5.1

This article was previously published under Q51495

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.

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbLangFortran KB51495