Page 1 of 1

Hex Codes in COBOL

PostPosted: Wed Jul 23, 2008 4:49 pm
by ritwik.das
Hi All,

Can anyone tell me in COBOL, how can I display the hex codes of the printable characters(a-z,@,# etc)?

Thanks!
Ritwik

Re: Hex Codes in COBOL

PostPosted: Wed Jul 23, 2008 5:47 pm
by jayind
Hi Ritwik,

I dont have the conversion logic but if you are not aware and want to see what is the HEX value of a character you can type "HEX ON" at the command prompt to see the character in HEX equivalent. To bring back to normal position type "HEX OFF". When you type "HEX ON" at the comman prompt and enter, it will show the equivalent which you need to read in the order top to bottom and left to right. For example,

Space - 4
        0

Null   -  0
          0

value positive 0 - F
                   0

etc..

Regards,
jayind

Re: Hex Codes in COBOL

PostPosted: Thu Jul 24, 2008 3:58 am
by dick scherrer
Hello,

First - there is no null value in cobol data. Every value from x'00' to x'ff' is a real value. Any book or teacher than calls x'00' a null is providing incorrect information.

One way to see the the hex values is to set up an array with values like:
01  hex-array.
    05 filler pic x(32) value '000102030405060708090A0B0C0D0E0F'.
       all of the other values. . .   
    05 filler pic x(32) value 'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'. 
01  hex-iitems redefines hex-array.
    05 hex-values occurs 256 times indexed by hex-inx.
       10 hex-over  pic x.
       10 hex-under  pic x.


You can then use the numeric value of each byte as the displacement into the array. If you build 2 arrays for output, you can create the "over" and "under" hex values and then present the hex data very similar to the way it can be seen using HEX ON.

There is another way to do this without the hex-array and if i can find it quickly, i'll post it also. I still use the array because it is in a callable routine that i've used since. . . well, a long, long time. . . And hex hasn't changed - works on all 3 major platforms (Win-based, unix, and mainframe) :)

Re: Hex Codes in COBOL

PostPosted: Fri Jul 25, 2008 10:18 am
by ritwik.das
Hi Dick!

Thanks for your inputs. I'm aware of the hex on/off feature but that will not serve my purpose here. I need to engineer a snippet of code that will accept any printable keyboard character as an input and print its hex value.

Your 2nd response was closer to satisfying my need. However I'm afraid that I did not quite understand it. Would you mind being a little bit more elaborate on this?

Thanks in advance!

Re: Hex Codes in COBOL

PostPosted: Fri Jul 25, 2008 9:43 pm
by dick scherrer
Hello,

You can use a more complete version of the array i postexd to do what you want. You need to complete the array so it has all of the values from x'00' to x'FF' - i only posted the first and last set of values.

Once you complete the array, you can use the binary value+1 of any byte (printable or not) as the displacement into the array and retrieve the "hex value" of that byte. How you present them is up to you. Most times, hex over/under is more readable - especially when there are multiple bytes to be shown.

Use this definition to convert a byte into a displacement into the array:
*                                       
 01  WORK-CHAR.                         
     05 FILLER PIC X.                   
     05 WC-X   PIC X.                   
 01  WORK-CHAR-N REDEFINES WORK-CHAR.   
     05 WC-N   PIC 99 COMP.             
Move the byte to wc-x and add 1 to wc-n. This is the subscript into the array to get the hex value for one byte.