Moving hexadecimal to a comp declared variable in cobol

Grekoz

Is it possible to assign a string of hexadecimal to a comp or binary declared variable?

Example: 01 COMP-VAR PIC 9(4) COMP.

MOVE X'04D2' TO COMP-VAR.

should output +1234.

Edited:

Sorry for the lack of the information, I just gave an example. The real scenario is that the data will come from an external source, a dataset. I need to store the data in an alphanumeric variable before I move it to a comp declared variable. My problem is that the data is incorrect when I move the alphanumeric data to the comp variable. Your help is very much appreciated.

NealB

I think you are looking for REDEFINES. Redefine the binary value as character, do the assignment which will not violate any of the assignment rules and then use the binary representation in subsequent operations. This program illustrates your example:

IDENTIFICATION DIVISION.
PROGRAM-ID. EXAMPLE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01.
   02 COMP-VAR PIC 9(4) COMP.
   02 COMP-X   REDEFINES COMP-VAR PIC X(2).
PROCEDURE DIVISION.
   MOVE X'04D2' TO COMP-X
   DISPLAY COMP-VAR
   GOBACK
   .

This displays 1234.

The larger question is why would you need to do this? I suspect that you are attempting to read a file with multiple record formats in it. Based on some common record identifier you need to read part of the record as character or as binary. Typically this is done a little differently in COBOL.

Here is a larger example of what I mean. Suppose you have an input record that is 3 bytes long. When the first byte is a 'B' it is telling you that the next two bytes should be treated as a binary (COMP) value. When the first byte is an 'X' you need to read the next two bytes as text (X) data. As an example this is what two records might look like:

 X'E7C1C2' 
 X'C204D2'

The first record is a text record containing the value 'AB' (EBCDIC). The second record is binary containing the value 1234. The program to process these records might look something like:

 IDENTIFICATION DIVISION.
 PROGRAM-ID. EXAMPLE.
 DATA DIVISION.
 WORKING-STORAGE SECTION.
 01  INPUT-RECORD.
     02 REC-TCD       PIC X.
        88 REC-TCD-BIN   VALUE 'B'.
        88 REC-TCD-CHAR  VALUE 'X'.
     02 REC-DUMMY        PIC X(2).
     02 REC-COMP-VAR  REDEFINES REC-DUMMY  PIC 9(4) BINARY.
     02 REC-CHAR-VAR  REDEFINES REC-DUMMY  PIC X(2).
 PROCEDURE DIVISION.
*
*    THIS IS A CHARACTER RECORD
*
     MOVE X'E7C1C2' TO INPUT-RECORD
     PERFORM DISPLAY-INPUT-RECORD
*
*    THIS IS A BINARY RECORD
*
     MOVE X'C204D2' TO INPUT-RECORD
     PERFORM DISPLAY-INPUT-RECORD
     GOBACK
     .
 DISPLAY-INPUT-RECORD.
     EVALUATE TRUE
         WHEN REC-TCD-BIN
              DISPLAY 'REC TYPE: '     REC-TCD
                      ' BINARY DATA: ' REC-COMP-VAR
         WHEN REC-TCD-CHAR
              DISPLAY 'REC TYPE: '     REC-TCD
                      ' CHAR DATA  : ' REC-CHAR-VAR
         WHEN OTHER
              DISPLAY 'UNKNOWN RECORD TYPE: ' REC-TCD
     END-EVALUATE
     . 

The output from this program is:

******************************** Top of Data ***********************************
REC-TYPE: X CHAR DATA  : AB
REC-TYPE: B BINARY DATA: 1234
******************************* Bottom of Data *********************************

Look at the INPUT-RECORD definition. The first byte determines how the rest of the record is to be intrepreted. REC-DUMMY is generally defined as a "generic" buffer area to be subsequently redefined. In the case of variable length input records, REC-DUMMY is defined to be as long as the longest record variant so the subsequent REDEFINEs of it do not "upset" the compiler. All data items following REC-DUMMY begin with the same level number (02 in the example) and REDEFINE it to the the appropriate format. Subsequent processing uses whatever record redefinition is appropaiate based on the value in REC-TCD.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related