Using the ICHAR and CHAR Intrinsic Functions

ID: Q51495

4.10 5.00 5.10 1.00 1.00a | 4.10 5.00 5.10 | 1.00 4.00

MS-DOS                    | OS/2           | WINDOWS NT
 kbocde

The information in this article applies to:

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: KBSubcategory: FORTLngIss

Keywords          : kbcode kbFortranPS kbLangFortran 
Version           : 4.10 5.00 5.10 1.00 1.00a | 4.10
Platform          : MS-DOS NT OS/2 WINDOWS

Last Reviewed: May 23, 1998