cobol

What is an easy way to get the ASCII value of a character in Cobol


We have to find more than one way to get the ascii value of a character. On top of that we also need to get the sum of all the characters's ascii values. I currently have the below and works alright for the first section where you need individual values . I just need to know if there is an easier way or a function to do this in Cobol?

DATA DIVISION.                                                 
FILE SECTION.                                                  

WORKING-STORAGE SECTION.                                       
01  WS-COUNTERS.                                               
    03  WS-COUNTER                  PIC  9(05).                
    03  WS-INPUT                    PIC  X(01).                
    03  WS-DISPLAY                  PIC  9(03).                

01  W1-ARRAY.                                                  
    03  ALPHABETIC-CHARS OCCURS 26 TIMES PIC X.                

01  W3-ARRAY.                                                  
    03  NUMERIC-CHARS OCCURS 26 TIMES PIC X.                   


PROCEDURE DIVISION.                                            
A000-MAIN SECTION.                                             
BEGIN.                                                         
    PERFORM B000-INITIALIZE.                                   
    PERFORM C000-PROCESS UNTIL WS-COUNTER > 26.                
    PERFORM D000-END.                                          

A099-EXIT.                                                     
    STOP RUN.                                                  

B000-INITIALIZE SECTION.                                       
    ACCEPT WS-INPUT.                                           
    MOVE "ABCDEFGHIJKLMNOPQRSTUVWXYZ" TO W1-ARRAY.             
    MOVE "01234567890000000000000000" TO W3-ARRAY.             
    MOVE 1 TO WS-COUNTER.                                      
    MOVE 0 TO WS-DISPLAY.                                      

B099-EXIT.                                                     
    EXIT.                                                      

C000-PROCESS SECTION.                                          
C001-BEGIN.                                                    
    IF WS-INPUT IS NUMERIC                                     
       IF NUMERIC-CHARS(WS-COUNTER) = WS-INPUT                 
          COMPUTE WS-DISPLAY = WS-COUNTER + 48 - 1             
       END-IF                                                  
    ELSE                                                       
       IF ALPHABETIC-CHARS(WS-COUNTER) = WS-INPUT              
          COMPUTE WS-DISPLAY = WS-COUNTER + 65 - 1             
       END-IF                                                  
    END-IF.                                                    

    ADD 1 TO WS-COUNTER.                                       


C099-EXIT.                                                     
    EXIT.                

Solution

  • Have a look at FUNCTION ORD and keep in mind that you will get the ordinal number in the program's collating sequence (which may be EBCDIC or not the full ASCII).

    As this function was introduced in the COBOL85 standard it should be available in most compilers (your question misses the compiler/machine you use).