View previous topic :: View next topic
|
Author |
Message |
bobachandra
New User
Joined: 21 Dec 2021 Posts: 2 Location: India
|
|
|
|
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'.
Please suggest me |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2133 Location: USA
|
|
|
|
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. |
|
Back to top |
|
|
bobachandra
New User
Joined: 21 Dec 2021 Posts: 2 Location: India
|
|
|
|
Hi,
Thanks for your reply.
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. |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2133 Location: USA
|
|
|
|
Are you banned from Google?
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. |
|
Back to top |
|
|
|