HEX to ASCII conversion

Go down

HEX to ASCII conversion

Post  kasthuri on Mon Jan 28, 2013 11:59 am

Hello Folks,

Tested a code for converting HEX values into ASCII format. Areas of usage would be places where in photos , finger prints, passwords etc need to be converted and stored into DB2 in ASCII(byte) format.

Since mainframes has no place for ASCII , you may have to receive data in Hex format and pass these values to below code and get the corresponding ASCII values. Every 2 chars of Hex gets converted to 1 char of ASCII(byte).

Again there are two types of HEX i.e. EBCDIC_Hex and ASCII_Hex.
So you may see two attachments which show the conversion tables, you may use either with the COBOL code below.

Code:
DCLGEN:
EXEC SQL DECLARE PHOTO_TAB TABLE
(
PHOTO VARCHAR(2900) NOT NULL
)

******************************
* COBOL DECLARATION FOR TABLE
******************************
01 TPHOTO.
10 PHOTO.
49 PHOTO-LEN PIC S9(4) USAGE COMP.
49 PHOTO-TEXT PIC X(2900).

******************************
WORKING-STORAGE SECTION.
******************************
05 CTR3 PIC 9(5) VALUE 0.
05 CTRH PIC 9(5) VALUE 0.

01 WS-PHOTO-HEX PIC X(5800).
01 WS-PHOTO-HEX-TWO PIC X(2).
01 WS-PHOTO-ASCII PIC X(2900).

PROCEDURE DIVISION.

0000-MAIN-PROCESS.

MOVE 1 TO CTRH
MOVE 1 TO CTR3
MOVE LENGTH OF PHOTO-TEXT TO PHOTO-LEN
MOVE WS-VALUE(1:5800) TO WS-PHOTO-HEX
MOVE FUNCTION UPPER-CASE(WS-VALUE(1:5800))
TO WS-PHOTO-HEX
PERFORM 3000-MAP-ASCII THRU 3000-MAP-ASCII-EXIT
VARYING CTRH FROM 1 BY 1 UNTIL CTRH > 5800


*--------------------------------------------------------*
* MAP INPUT HEX TO ASCII *
*--------------------------------------------------------*

3000-MAP-ASCII.

MOVE WS-PHOTO-HEX(CTRH:2) TO WS-PHOTO-HEX-TWO.
COPY HEX2ASCII.
* COPY HEXTASCI
ADD 1 TO CTRH.
ADD 1 TO CTR3.

3000-MAP-ASCII-EXIT.
EXIT.


MOVE WS-PHOTO-ASCII TO CMNCS-PHOTO-TEXT.

*--------------------------------------------------------*
* INSERT RECORD INTO PHOTO_TABLE *
*--------------------------------------------------------*
4000-INSERT-REC.

EXEC SQL
INSERT INTO PHOTO_TAB
(
PHOTO
)
VALUES
(
:PHOTO
)
END-EXEC.

STOP-RUN


Code'd

kasthuri

Posts : 212
Join date : 2013-01-27

View user profile

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum