# convert alphanumeric PIC X(02) to hex value 9(01) COMP-3

Author Message
bobachandra

New User

Joined: 21 Dec 2021
Posts: 2
Location: India

Posted: Tue Dec 21, 2021 10:47 am

I have one requirement, where I have Alphanumeric value = '86' and its length is defined as PIC x(02). I need to convert it into hex x'86' and its length is defined as PIC 9(01) comp-3.

example:-

 Code: 01 WS-ALPHANUMERIC PIC X(02) VALUE '86'. 01 WS-HEX PIC 9(01) COMP-3. PROCEDURE DIVISION. MOVE WS-ALPHANUMERIC TO WS-HEX. DISPLAY WS-HEX. STOP RUN

I am getting x'FF' in my spool. But I am expecting x'86'.

sergeyken

Senior Member

Joined: 29 Apr 2008
Posts: 2081
Location: USA

 Posted: Wed Dec 22, 2021 1:10 am Use PIC 9(02) instead of PIC X(02) P.S. Anyway, you'll never get x'86' as your result. PIC 9(01) COMP-3 has nothing to do with your HEX expectations. It is not clear: what is your goal in this example? PIC X(02) VALUE '86' is x'F8F6' PIC 9(02) VALUE 86 is x'F8C6' PIC 9(02) COMP-3 VALUE 86 is x'086C' PIC 9(01) COMP-3 VALUE 86 is x'6C' etc.
bobachandra

New User

Joined: 21 Dec 2021
Posts: 2
Location: India

Posted: Wed Dec 22, 2021 4:13 pm

Hi,

Basically I have binary value '1000 0110'.

 Code: 01 ws-binary         PIC   X(08) '10000110'. 01 ws-numeric      PIC   9(02). 01 ws-hex            PIC    9(01) comp-3.

when we convert above values into hex format with below logic.
Binary of 1000 = 8 (hex value)
Binary of 0110 = 6 (hex value)

 Code: IF ws-binary(1:4) = '1000'    move 8  to ws-numeric(1:1) END-IF IF ws-binary(5:4) = '0110'     move 6 to ws-numeric(2:1) END-IF move ws-numeric     to ws-hex

Final output I am expecting is like below

f - hex value
8 - enter HEX ON, In top it should show 8
6 - , In Bottom it should show 6.

I hope above explanation is clear.
sergeyken

Senior Member

Joined: 29 Apr 2008
Posts: 2081
Location: USA

Posted: Wed Dec 22, 2021 7:50 pm

How to display in hexadecimal using COBOL

Problem

Convert a hex number such as CA84 or a binary number defined as USAGE IS COMPUTATIONAL into a printable hex string in EBCDIC.

Cause

COBOL has no converter. A program is needed.

Resolving The Problem

This is just an illustration. It's not guaranteed.
 Code: 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: Hex           d       ----  This is all you get DISPLAY HEXNUM Dec        00051844   ----  Decimal equivalent of X'CA84' Printable  0000CA84   ----  After conversion.

First of all, before starting to code something, you strictly need to begin learning the basics of Computer Science, and Mainframe Principles of Operations.
 View Bookmarks All times are GMT + 6 Hours

 Topic Forum Replies Similar Topics Need to convert date format DFSORT/ICETOOL 20 Keep leading zero(s) after convert fl... SYNCSORT 7 COBOL - Move S9(11)v9(7) COMP-3 to -(... COBOL Programming 5 Convert single row multi cols to sing... DFSORT/ICETOOL 6 convert file from VB to FB and use tr... DFSORT/ICETOOL 8
Search our Forums:

 IBMMainframes.com is not an official and/or affiliated with IBM® in anyway Board Rules | FAQ | Downloads | Wiki | SiteMap | Contact Us