Microsoft KB Archive/39365

From BetaArchive Wiki

REDEFINES COMP-0 Converts PIC X Field to ASCII Value; COBOL

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

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

The information in this article applies to:
- Microsoft COBOL for MS-DOS and OS/2, versions 3.0 and 3.0a

Summary: The Microsoft COBOL Version 3.0 routine below demonstrates a method for putting the ASCII value of a character into a numeric field. This is accomplished by using the REDEFINES clause to overlay the text field with a numeric field. Because the text field and the numeric field are referencing the same place in memory, the numeric field will take on the ASCII value of the text in the field.

More Information: A COMP-0 numeric field is two-bytes long and stored in high-byte, low-byte format. Because a single character only takes one byte to store, the text field needs to be padded with an extra character. The extra character needs to be set to a binary zero using the LOW-VALUES clause so it does not interfere with the ASCII value returned in the numeric field. The following is an example COBOL program that may be compiled and LINKed accepting the defaults: This COBOL program will accept a character from the keyboard and display its ASCII value. WORKING-STORAGE SECTION. 01 FIELD-1. 05 DUMMY-FIELD PIC X VALUE LOW-VALUES. 05 CONVERT-FIELD PIC X VALUE SPACES. 77 FIELD-2 REDEFINES FIELD-1 PIC S9(4) COMP-0. PROCEDURE DIVISION. ACCEPT CONVERT-FIELD. DISPLAY FIELD-2. STOP RUN.

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