Hi, I have no understanding of Cobol but got a program to understand so that I can implement similar logic in my database.
Can someone please help me with sharing what's happening at each line of the code below? There are two sections: One to convert from Base10 to 32 and vice versa.
Cobol code –
MYLP15-CONVERSION SECTION.
MOVE 0 TO LY15-RETURN-CODE
MOVE SPACES TO LY15-OUTPUT.
IF LY15-10TO32
PERFORM MYLP15-CONVERT-10TO32
END-IF.
IF LY15-32TO10
PERFORM MYLP15-CONVERT-32TO10
END-IF.
MYLP15-CONVERT-10TO32 SECTION.
MOVE LY15-INPUT-BASE10 TO LY15-NUMBER.
MOVE 0 TO LY15-I.
PERFORM
WITH TEST AFTER
UNTIL LY15-NUMBER NOT > 0
DIVIDE LY15-NUMBER BY 32
GIVING LY15-NUMBER
REMAINDER LY15-REMAINDER
MOVE LY15-OUTPUT TO LY15-SAVE
MOVE LY15-HEXNUMBER(LY15-REMAINDER + 1)
TO LY15-OUTPUT(1:1)
MOVE LY15-SAVE
TO LY15-OUTPUT(2:9)
END-PERFORM.
MYLP15-CONVERT-32TO10 SECTION.
MOVE 0 TO LY15-NUMBER.
MOVE 0 TO LY15-I.
MOVE LENGTH OF LY15-INPUT-BASE32 TO LY15-LEN.
PERFORM
WITH TEST AFTER
VARYING LY15-LEN FROM LY15-LEN BY -1
UNTIL LY15-INPUT-BASE32(LY15-LEN:1) GREATER THAN ' '
OR LY15-LEN = 0
END-PERFORM.
IF LY15-LEN = 0
MOVE 1 TO LY15-RETURN-CODE
ELSE
PERFORM
VARYING LY15-J FROM 1 BY 1
UNTIL LY15-J > LY15-LEN
OR LY15-RETURN-CODE NOT = 0
MULTIPLY LY15-NUMBER BY 32
GIVING LY15-NUMBER
PERFORM
WITH TEST AFTER
VARYING LY15-I FROM 1 BY 1
UNTIL LY15-HEXNUMBER(LY15-I) =
LY15-INPUT-BASE32(LY15-J:1)
OR LY15-I = 32
END-PERFORM
IF LY15-HEXNUMBER(LY15-I) NOT =
LY15-INPUT-BASE32(LY15-J:1)
MOVE 2 TO LY15-RETURN-CODE
ELSE
COMPUTE LY15-NUMBER = LY15-NUMBER + LY15-I - 1
END-IF
END-PERFORM
MOVE LY15-NUMBER TO LY15-OUTPUT-BASE10
END-IF.
MYLP15-CONVERSION-END SECTION.
EXIT.
Can someone please help me with sharing what's happening at each line of the code below? There are two sections: One to convert from Base10 to 32 and vice versa.
Cobol code –
MYLP15-CONVERSION SECTION.
MOVE 0 TO LY15-RETURN-CODE
MOVE SPACES TO LY15-OUTPUT.
IF LY15-10TO32
PERFORM MYLP15-CONVERT-10TO32
END-IF.
IF LY15-32TO10
PERFORM MYLP15-CONVERT-32TO10
END-IF.
MYLP15-CONVERT-10TO32 SECTION.
MOVE LY15-INPUT-BASE10 TO LY15-NUMBER.
MOVE 0 TO LY15-I.
PERFORM
WITH TEST AFTER
UNTIL LY15-NUMBER NOT > 0
DIVIDE LY15-NUMBER BY 32
GIVING LY15-NUMBER
REMAINDER LY15-REMAINDER
MOVE LY15-OUTPUT TO LY15-SAVE
MOVE LY15-HEXNUMBER(LY15-REMAINDER + 1)
TO LY15-OUTPUT(1:1)
MOVE LY15-SAVE
TO LY15-OUTPUT(2:9)
END-PERFORM.
MYLP15-CONVERT-32TO10 SECTION.
MOVE 0 TO LY15-NUMBER.
MOVE 0 TO LY15-I.
MOVE LENGTH OF LY15-INPUT-BASE32 TO LY15-LEN.
PERFORM
WITH TEST AFTER
VARYING LY15-LEN FROM LY15-LEN BY -1
UNTIL LY15-INPUT-BASE32(LY15-LEN:1) GREATER THAN ' '
OR LY15-LEN = 0
END-PERFORM.
IF LY15-LEN = 0
MOVE 1 TO LY15-RETURN-CODE
ELSE
PERFORM
VARYING LY15-J FROM 1 BY 1
UNTIL LY15-J > LY15-LEN
OR LY15-RETURN-CODE NOT = 0
MULTIPLY LY15-NUMBER BY 32
GIVING LY15-NUMBER
PERFORM
WITH TEST AFTER
VARYING LY15-I FROM 1 BY 1
UNTIL LY15-HEXNUMBER(LY15-I) =
LY15-INPUT-BASE32(LY15-J:1)
OR LY15-I = 32
END-PERFORM
IF LY15-HEXNUMBER(LY15-I) NOT =
LY15-INPUT-BASE32(LY15-J:1)
MOVE 2 TO LY15-RETURN-CODE
ELSE
COMPUTE LY15-NUMBER = LY15-NUMBER + LY15-I - 1
END-IF
END-PERFORM
MOVE LY15-NUMBER TO LY15-OUTPUT-BASE10
END-IF.
MYLP15-CONVERSION-END SECTION.
EXIT.