The below COBOL program takes a string as input and displays the hexadecimal value of each byte of that string.
WORKING-STORAGE SECTION.
01 HEXIN PIC X(4).
01 HEXVAL PIC X(8).
01 HEXSTR PIC X(16) VALUE "0123456789ABCDEF".
01 DEC PIC S9(4) COMP.
01 FILLER REDEFINES DEC.
02 FILLER PIC X.
02 DECBYTE PIC X.
01 I PIC S9(8) COMP.
01 J PIC S9(8) COMP.
01 Q PIC S9(8) COMP.
01 R PIC S9(8) COMP.
01 J1 PIC S9(8) COMP.
01 Q1 PIC S9(8) COMP.
01 R1 PIC S9(8) COMP.
PROCEDURE DIVISION.
MOVE 'ABCD' TO HEXIN.
PERFORM CONVERT
DISPLAY "HEX IN : " HEXIN
DISPLAY "PRINTABLE : " HEXVAL
GOBACK.
CONVERT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4
COMPUTE J = 2 * I - 1
MOVE HEXIN(I:1) TO DECBYTE
DIVIDE DEC BY 16 GIVING Q REMAINDER R
COMPUTE J1 = J + 1
COMPUTE Q1 = Q + 1
COMPUTE R1 = R + 1
MOVE HEXSTR(Q1:1) TO HEXVAL(J:1)
MOVE HEXSTR(R1:1) TO HEXVAL(J1:1)
END-PERFORM.
Output of the above program is below
HEX IN : ABCD
PRINTABLE : C1C2C3C4
WORKING-STORAGE SECTION.
01 HEXIN PIC X(4).
01 HEXVAL PIC X(8).
01 HEXSTR PIC X(16) VALUE "0123456789ABCDEF".
01 DEC PIC S9(4) COMP.
01 FILLER REDEFINES DEC.
02 FILLER PIC X.
02 DECBYTE PIC X.
01 I PIC S9(8) COMP.
01 J PIC S9(8) COMP.
01 Q PIC S9(8) COMP.
01 R PIC S9(8) COMP.
01 J1 PIC S9(8) COMP.
01 Q1 PIC S9(8) COMP.
01 R1 PIC S9(8) COMP.
PROCEDURE DIVISION.
MOVE 'ABCD' TO HEXIN.
PERFORM CONVERT
DISPLAY "HEX IN : " HEXIN
DISPLAY "PRINTABLE : " HEXVAL
GOBACK.
CONVERT.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 4
COMPUTE J = 2 * I - 1
MOVE HEXIN(I:1) TO DECBYTE
DIVIDE DEC BY 16 GIVING Q REMAINDER R
COMPUTE J1 = J + 1
COMPUTE Q1 = Q + 1
COMPUTE R1 = R + 1
MOVE HEXSTR(Q1:1) TO HEXVAL(J:1)
MOVE HEXSTR(R1:1) TO HEXVAL(J1:1)
END-PERFORM.
Output of the above program is below
HEX IN : ABCD
PRINTABLE : C1C2C3C4
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.