How to convert EBCDIC to ASCII or ASCII to EBCDIC in a COBOL Program?

Technote (FAQ)


Question

Within an Enterprise COBOL for z/OS program, how do I convert an item described as PICTURE X, which is EBCDIC on the mainframe, to ASCII?

Cause

Use the FUNCTION NATIONAL-OF and FUNCTION DISPLAY-OF. It is necessary to first convert it to Unicode. In the following example, WS-CCSID must match the code page of the data.

Answer

This simple example is for Enterprise COBOL on z/OS, not for COBOL on AIX. It only illustrates the FUNCTION NATIONAL-OF and FUNCTION DISPLAY-OF so it may not be complete enough for your application.

CBL DBCS,NSYMBOL(NATIONAL)
IDENTIFICATION DIVISION.                        
PROGRAM-ID. MAIN1.                            
ENVIRONMENT DIVISION.                            
DATA DIVISION.                                    
WORKING-STORAGE SECTION.                          
01  WS-AREA          PICTURE N(50).                
01  WS-CCSID       PICTURE 9(5).                  
01  WS-ASCII         PICTURE X(50).                
01  WS-EBCDIC      PICTURE X(50).                
PROCEDURE DIVISION.                              
    MOVE "ABCDE"                    
         TO WS-EBCDIC.                            
         DISPLAY " WS-EBCDIC   = " WS-EBCDIC.    
    MOVE FUNCTION NATIONAL-OF(WS-EBCDIC)          
         TO WS-AREA.                              
         DISPLAY " WS-AREA     = " WS-AREA.      
    MOVE 819     TO WS-CCSID.                    
    MOVE FUNCTION DISPLAY-OF(WS-AREA, WS-CCSID)  
         TO WS-ASCII.    
       DISPLAY " WS-ASCII    = " WS-ASCII.
    DISPLAY "Now let's convert that WS-ASCII back to EBCDIC".
    MOVE FUNCTION NATIONAL-OF(WS-ASCII,819 )                
     TO WS-AREA.                                        
     DISPLAY " WS-AREA     = " WS-AREA.                  
    MOVE 1140    TO WS-CCSID.                                
    MOVE FUNCTION DISPLAY-OF(WS-AREA, WS-CCSID)              
     TO WS-EBCDIC.                                      
    DISPLAY " WS-EBCDIC   = " WS-EBCDIC.                  
    STOP RUN.  
     
The EBCDIC letters ABCDE are hex C1C2C3C4C5. They will be converted to hex 4142434445. Then the 4142434445 will be converted back to C1C2C3C4C5.

Related information

Programming Guide example

Rate this page:

(0 users)Average rating

Document information


More support for:

Enterprise COBOL for z/OS
Compile

Software version:

3.4, 4.1, 4.2

Operating system(s):

z/OS

Reference #:

1229962

Modified date:

2006-03-27

Translate my page

Machine Translation

Content navigation