Microsoft KB Archive/58566

REDEFINES in COBOL 3.0/3.0a Can Mimic BASIC’s ASC and CHR$

PSS ID Number: Q58566 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: Microsoft COBOL Compiler Versions 3.0 and 3.0a do not have a statement that directly converts between characters (PIC X) and their numeric ASCII representations. However, you can write a program using COBOL’s REDEFINES clause to do this conversion. (This conversion is equivalent to that done by the CHR$ and ASC functions found in Microsoft BASIC.)

More Information: By using the REDEFINES clause, the same variable (memory location) can be referenced using different formats. By using a PIC X for a character and a PIC 99 COMP-X, BASIC’s CHR$ and ASC functions are simulated in COBOL by moving information into one data-name and checking the other. Note that BASIC’s CHR$(num) function returns a one-character string whose ASCII code is num. BASIC's ASC(stringargument) function returns a numeric value that is the ASCII code for the first character in the stringargument. The following code example illustrates how to use COBOL's REDEFINES clause to simulate BASIC's ASC and CHR$ functions: WORKING-STORAGE SECTION. 01 CHR PIC X. 01 ASC PIC 99 COMP-X REDEFINES CHR. PROCEDURE DIVISION. 000-MAIN. **** equivalent to ASC(“X”) in BASIC MOVE “X” TO CHR. DISPLAY “ASCII VALUE OF X:”. DISPLAY ASC. **** equivalent to CHR$(89) MOVE 89 TO ASC. DISPLAY “CHAR FOR ASCII 89:”. DISPLAY CHR. STOP RUN. The output of the above program is as follows: ASCII VALUE OF X: 088 CHAR FOR ASCII 89: Y

Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.