Any Hex function for COBOL available?

OS/VS COBOL, COBOL II, Enterprise COBOL for z/OS. OpenCOBOL and OOCobol.
Post Reply
Mukesh Chhabra
New Member
Posts: 6
Joined: Tue Feb 02, 2016 10:38 am

Any Hex function for COBOL available?

Post by Mukesh Chhabra »

HI -

It is surprising to know that when I was searching for a COBOL intrinsic functions to convert a number to hex and NOT to find anything that will convert a number to Hexadecimal.

Does anybody have a routine that can take in a NUMBER (up to 10 digits) and produce a Hex string from it?
William Collins
Global Moderator
Global Moderator
Posts: 490
Joined: Sun Aug 25, 2013 7:24 pm

Re: Any Hex function for COBOL available?

Post by William Collins »

Why do you want to do this, specifically?
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Any Hex function for COBOL available?

Post by Robert Sample »

You need to provide some sample input and expected output since I'm not sure what you mean by "Hex string". A COBOL PIC S9(10) COMP-5 variable contains an 8-byte Hex value that is the binary value of the number; PIC 9(10) produces a 10-byte Hex string that has the value (X'F0F1F2F3F4F5F6F7F8F9' for 0123456789, for example). Remember that COBOL does NOT have "strings" in the sense that C/C++ has strings (COBOL variables are not, as a rule, variable length at run time even though there are a few cases where they can be).
Mukesh Chhabra
New Member
Posts: 6
Joined: Tue Feb 02, 2016 10:38 am

Re: Any Hex function for COBOL available?

Post by Mukesh Chhabra »

This is nothing much than an intermediate exercise to see the values we are getting. There is no production release of this we have planned as of now.

While searching I have got this link, http://www-01.ibm.com/support/docview.w ... wg21177358, which gives some hints in the form of program below:

Code: Select all

IDENTIFICATION DIVISION.                       
PROGRAM-ID. HEXPRINT. 
*REMARKS.  CONVERT FROM BINARY TO PRINTABLE HEX.
ENVIRONMENT DIVISION.                          
DATA DIVISION.                                 
WORKING-STORAGE SECTION. 
* HEXVAL (output) must be twice the size of HEXNUM (input).
* For example if you have 20 bytes of garbage but
* want to display it in hex, change the picture
* of HEXNUM to X(20), of HEXVAL to X(40), then
* move the garbage to HEXNUM. Ignore DECNUM.
01  HEXNUM   PIC X(4) VALUE X"0000CA84".       
01  DECNUM   REDEFINES HEXNUM PIC S9(8) COMP.  
01  HEXVAL   PIC X(8).                          
01  HEXSTR   PIC X(16) VALUE "0123456789ABCDEF".
01  DEC      PIC S9(4) COMP.                    
01  FILLER   REDEFINES DEC.                     
    02  FILLER PIC X.                           
    02  DECBYTE PIC X.                          
01  I   PIC S9(8) COMP.                         
01  J   PIC S9(8) COMP.                         
01  Q   PIC S9(8) COMP.                         
01  R   PIC S9(8) COMP.                         
01  J1  PIC S9(8) COMP.                         
01  Q1  PIC S9(8) COMP.                         
01  R1  PIC S9(8) COMP.                         
PROCEDURE DIVISION.                              
TEST-IT.        
    DISPLAY "Hex        " HEXNUM.               
    DISPLAY "Dec        " DECNUM.              
    PERFORM CONVERT                             
    DISPLAY "Printable  " HEXVAL.
    GOBACK.                                   
CONVERT.                                      
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > LENGTH OF HEXNUM 
      COMPUTE J = 2 * I - 1                   
      MOVE HEXNUM(I:1) TO DECBYTE             
      DIVIDE DEC BY 16 GIVING Q REMAINDER R   
      COMPUTE J1 = J + 1                      
      COMPUTE Q1 = Q + 1                      
      COMPUTE R1 = R + 1                      
      MOVE HEXSTR(Q1:1) TO HEXVAL(J:1)        
      MOVE HEXSTR(R1:1) TO HEXVAL(J1:1)       
    END-PERFORM. 
_________Results_______

Code: Select all

Hex           d       ----  This is all you get DISPLAY HEXNUM
Dec        00051844   ----  Decimal equivalent of X'CA84'
Printable  0000CA84   ----  After conversion.
But then there is no intrinsic function..
User avatar
Robert Sample
Global Moderator
Global Moderator
Posts: 1895
Joined: Fri Jun 28, 2013 1:22 am
Location: Dubuque Iowa
United States of America

Re: Any Hex function for COBOL available?

Post by Robert Sample »

But then there is no intrinsic function
No, there is no intrinsic function to do this.
Post Reply

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “IBM COBOL, GnuCOBOL (OpenCOBOL), OOCobol.”