Problem:
Using "inspect ... converting" to convert signed, EBCDIC numeric data to signed, ASCII numeric data.
Resolution:
The example program, "CONVERTING.cbl" (see attachement). illustrates how to use an "inspect ... converting" statement to translate EBCDIC numeric display values to equivalent ASCII numeric display values. The following syntax is used.
INSPECT identifier-1 CONVERTING identifier-6 TO identifier-7
"identifier-1" is a pic x(9) item initialized to a hexadecimal value 'F1F2F3F4F5F6F7F8D9' that is equivalent to "123456789-", a signed, numeric display value on an EBCDIC computer system.
"identifier-6" is a table of the numeric EBCDIC characters that might appear in a signed, numeric display item. There are three groups of possibilities. The first group, (C0C1...), are positive, unsigned digits. The second group, (D0D1...), are negative, signed digits. The third group, (F0F1...) are plain digits. The signed and unsigned possibilties only exist in the units (least significant) possition.
'C0C1C2C3C4C5C6C7C8C9D0D1D2D3D4D5D6D7D8D9F0F1F2F3F4F5F6F7F8F9'
"identifier-7" is a second table that defines the ASCII equivalent of the EBCDIC table. Micro Focus COBOL doesn't support a negative zero so you will see a "30" in the position that corresponds to the "D0" in
the EBCDIC table. You'll also note that the plain ASCII digits appear twice in the table as unsigned digits are not distinguished from plain digits on ASCII sstems.
'303132333435363738393071727374757677787930313233343536373839'
The complete source of the example program follows.
working-storage section.
77 dtItem pic x(9) value x'F1F2F3F4F5F6F7F8D9'.
77 dataItem redefines dtItem pic s9(9).
78 ebcdicDigits value
x'C0C1C2C3C4C5C6C7C8C9D0D1D2D3D4D5D6D7D8D9F0F1F2F3F4F5F6F7F8F9'.
78 asciiDigits value
x'303132333435363738393071727374757677787930313233343536373839'.
procedure division.
display dtItem
inspect dtItem converting ebcdicDigits to asciiDigits
display dtItem
display dataItem
exit program
stop run.
dtItem: a pic x(9) display item initialized to EBCDIC characters that are appropriate for a signed, numeric display item.
dataItem: a pic s9(9) display item that redefines dtItem.
ebcdicDigits: The EBCDIC characters that may exist in an numeric display value.
asciiDigits: The ASCII characters that correspond to the EBCDIC characters. In ASCII there are really only two sets of characters, negative signed and unsigned.
The inspect statement first locates a matching EBCDIC character in the EBCDIC table and then retrieves, from the corresponding location in the ASCII table, the equivalent ASCII character value. It then overlays
the EBCDIC value with the ASCII value.
See Related tab for attched example.
