Hex Codes in COBOL



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Hex Codes in COBOL

Postby ritwik.das » Wed Jul 23, 2008 4:49 pm

Hi All,

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

Thanks!
Ritwik
Thanks & Regards!
Ritwik
ritwik.das
 
Posts: 13
Joined: Wed Jul 16, 2008 10:27 am
Has thanked: 0 time
Been thanked: 0 time

Re: Hex Codes in COBOL

 

Re: Hex Codes in COBOL

Postby jayind » Wed Jul 23, 2008 5:47 pm

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
jayind
 
Posts: 62
Joined: Wed Apr 23, 2008 1:37 pm
Location: Chennai, India
Has thanked: 0 time
Been thanked: 0 time

Re: Hex Codes in COBOL

Postby dick scherrer » Thu Jul 24, 2008 3:58 am

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) :)
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times

Re: Hex Codes in COBOL

Postby ritwik.das » Fri Jul 25, 2008 10:18 am

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!
Thanks & Regards!
Ritwik
ritwik.das
 
Posts: 13
Joined: Wed Jul 16, 2008 10:27 am
Has thanked: 0 time
Been thanked: 0 time

Re: Hex Codes in COBOL

Postby dick scherrer » Fri Jul 25, 2008 9:43 pm

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.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6304
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 91 times


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post