IBM Support

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

Question & Answer


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

[{"Product":{"code":"SS6SG3","label":"Enterprise COBOL for z\/OS"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Component":"Compile","Platform":[{"code":"PF035","label":"z\/OS"}],"Version":"3.4;4.1;4.2","Edition":"","Line of Business":{"code":"LOB17","label":"Mainframe TPS"}}]

Document Information

Modified date:
08 August 2018

UID

swg21229962