IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

convert hex data to char


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
lcmontanez

New User


Joined: 19 Jun 2007
Posts: 50
Location: Chicago

PostPosted: Sat Jan 17, 2009 1:09 am
Reply with quote

Is there a function or technique to convert a X'10', X'11', x'12, etc to
char/numeric 10, 11. 12, etc?
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 17, 2009 2:14 am
Reply with quote

Hello,

Yes, set up an array of 2-byte entries with values from '00' to 'FF'.

Use the byte with the hex-value as the displacement into the array and you have the value for any possible one-byte hex-value.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Sat Jan 17, 2009 2:44 am
Reply with quote

Where are these "HEX" values coming from?
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Jan 17, 2009 3:22 am
Reply with quote

You can also resolve the two-byte equivalent of the one-byte hex-value on the fly. As your requirement states, the HEX-VALUE can be in the range of X'00' through X'FF', resolving to a two-byte CHARACTER value of C'00' through C'FF'.

Code:

03  WS-HEX-BYTE PIC X(01).
03  WS-PACKED PIC 9(03) COMP-3.
03  WS-PACKED-V9 REDEFINES WS-PACKED PIC 9(02)V9 COMP-3.
03  WS-PACKED-X REDEFINES WS-PACKED PIC X(02).
03  WS-DISPLAY PIC 9(03).
03  WS-DISPLAY-V9 REDEFINES WS-DISPLAY PIC 9(02)V9.
03  WS-DISPLAY-X REDEFINES WS-DISPLAY PIC X(03).

MOVE X'FF' TO WS-HEX-BYTE.
MOVE ZERO TO WS-PACKED.
MOVE WS-HEX-BYTE TO WS-PACKED-X (1:1).
MOVE WS-PACKED-V9 TO WS-DISPLAY-V9.
INSPECT WS-DISPLAY-X CONVERTING X'FAFBFCFDFEFF' TO 'ABCDEF'.

In this example and after the INSPECT, WS-DISPLAY-X (1:2) will equal C'FF'. Byte three will always equal C'0'.

(I know Jack's going to get after me about this) icon_wink.gif

HTH....

Regards,

Bill
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Sat Jan 17, 2009 5:49 am
Reply with quote

dick scherrer wrote:
Hello,

Yes, set up an array of 2-byte entries with values from '00' to 'FF'.

Use the byte with the hex-value as the displacement into the array and you have the value for any possible one-byte hex-value.


Dick,

Assuming that a WS binary-halfword is first cleared to H'0' and then a 2-byte redefinition of PIC XX is defined over this halfword, the hex-value is moved to the 2nd-Byte. Then the halfword needs to be bumped by 01, because the table would have 256-entries (C'00' through C'FF', beginning with entry 01).

Regards,

Bill
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Sat Jan 17, 2009 7:28 am
Reply with quote

Hi Bill,

Quote:
. . .Then the halfword needs to be bumped by 01. . .

Yup, this has been working for longer than i prefer to admit. . . If i've not messed up the copy/paste, it is a bit of callable code that will return the hex over/under for a string of up to 2000 bytes. The calling code provides the number of bytes and the string to be converted to hex.

Code:

      *---------------------------------------------------------------*
      *                                                               *
      * THIS CODE WILL CONVERT AN INPUT STRING TO HEX OVER/UNDER      *
      *  REPRESENTATION FOR DEBUGGING/READABILITY.                    *
      *                                                               *
      *---------------------------------------------------------------*

       77  WKN                 PIC 9(5) COMP.                           
      *                                                                 
       01  HEX-VALUES.                                                 
           05 FILLER PIC X(32) VALUE '000102030405060708090A0B0C0D0E0F'.
           05 FILLER PIC X(32) VALUE '101112131415161718191A1B1C1D1E1F'.
           05 FILLER PIC X(32) VALUE '202122232425262728292A2B2C2D2E2F'.
           05 FILLER PIC X(32) VALUE '303132333435363738393A3B3C3D3E3F'.
           05 FILLER PIC X(32) VALUE '404142434445464748494A4B4C4D4E4F'.
           05 FILLER PIC X(32) VALUE '505152535455565758595A5B5C5D5E5F'.
           05 FILLER PIC X(32) VALUE '606162636465666768696A6B6C6D6E6F'.
           05 FILLER PIC X(32) VALUE '707172737475767778797A7B7C7D7E7F'.
           05 FILLER PIC X(32) VALUE '808182838485868788898A8B8C8D8E8F'.
           05 FILLER PIC X(32) VALUE '909192939495969798999A9B9C9D9E9F'.
           05 FILLER PIC X(32) VALUE 'A0A1A2A3A4A5A6A7A8A9AAABACADAEAF'.
           05 FILLER PIC X(32) VALUE 'B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF'.
           05 FILLER PIC X(32) VALUE 'C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF'.
           05 FILLER PIC X(32) VALUE 'D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF'.
           05 FILLER PIC X(32) VALUE 'E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF'.
           05 FILLER PIC X(32) VALUE 'F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF'.
       01  HEX-VALUES-R REDEFINES HEX-VALUES.                           
           05 HEX-CHAR     OCCURS 256 TIMES                             
                           INDEXED BY HX-INX.                           
              10 HEX-OVER  PIC X.                                       
              10 HEX-UNDER PIC X.                                       
      *                                                                 
       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.                                       

       LINKAGE SECTION.                                           
      *                                                           
       01  THE-NUMBER        PIC 9(5) COMP.                       
      *                                                           
       01  ORIGINAL-LINE.                                         
           05 ORIG-CHAR      PIC X OCCURS 2000 TIMES               
                             INDEXED BY ORG-INX.                   
      *                                                           
       01  HEX-OVER-LINE.                                         
           05 HEX-OVER-CHAR  PIC X OCCURS 2000 TIMES               
                             INDEXED BY HO-INX.                   
      *                                                           
       01  HEX-UNDER-LINE.                                         
           05 HEX-UNDER-CHAR PIC X OCCURS 2000 TIMES               
                             INDEXED BY HU-INX.                   
      *                                                           
       PROCEDURE DIVISION USING THE-NUMBER,                       
                                ORIGINAL-LINE,                     
                                HEX-OVER-LINE,                     
                                HEX-UNDER-LINE.                   
      *                                                           
           MOVE LOW-VALUES TO WORK-CHAR.                           
      *                                                           
           PERFORM 050-DUMMY THRU 059-DUMMY-EXIT                   
                             VARYING ORG-INX FROM 1 BY 1           
                             UNTIL ORG-INX > THE-NUMBER.           
           GO TO 999-END.                                         
      *                                                           
       050-DUMMY.                                                 
           MOVE ORIG-CHAR(ORG-INX) TO WC-X.                       
      *    DISPLAY WORK-CHAR ' ' WC-X ' ' WC-N.                   
           COMPUTE WKN = WC-N + 1.                                 
           SET HX-INX TO WKN.                                     
           SET HO-INX TO ORG-INX.                                 
           MOVE HEX-OVER(HX-INX) TO HEX-OVER-CHAR(HO-INX).         
           SET HU-INX TO ORG-INX.                                 
           MOVE HEX-UNDER(HX-INX) TO HEX-UNDER-CHAR(HU-INX).       
      *                                                                     
       059-DUMMY-EXIT.     
           EXIT.           
      *                     
       999-END.             
           GOBACK.         
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts How to save SYSLOG as text data via P... All Other Mainframe Topics 4
No new posts Store the data for fixed length COBOL Programming 1
No new posts SFTP Issue - destination file record ... All Other Mainframe Topics 2
No new posts Data set Rec-Cnt and Byte-Cnt Testing & Performance 2
No new posts SCOPE PENDING option -check data DB2 2
Search our Forums:

Back to Top