0

I'm trying to compare two hex values as in the code at the bottom. I was expecting that the IF statement, where I compare field A and field WS-FLD-X, would result in true, but it doesn't do it.

In other words, when I move 12 to WS-FLD-A, the value in WS-FLD-X should be stored as X'0C', right? This value is expected to be the same value in field A. Comparing the two values should result in true, however this is not happening.

Why? What is the difference between the value held in field A and the value in WS-FLD-X?

IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 FF.
    05  A  PIC XX.
    05  B  PIC XXXXXX.
01 F.
    05 WS-FLD PIC S9(4) COMP.
    05 WS-FLD-X REDEFINES WS-FLD PIC XX.

PROCEDURE DIVISION.
    DISPLAY 'Hello, world' UPON CONSOLE.

    MOVE X'0C' TO A.
    MOVE "SOME TEXT" TO B.
    DISPLAY FF UPON CONSOLE.

    MOVE 12 TO WS-FLD
    DISPLAY "HEX OF 12 IS:" WS-FLD-X UPON CONSOLE.
    IF WS-FLD-X = A THEN DISPLAY "SAME" UPON CONSOLE END-IF.

Code in web IDE

Jorengarenar
  • 2,705
  • 5
  • 23
  • 60
NoChance
  • 5,632
  • 4
  • 31
  • 45
  • 2
    I'm not sure what actual question this post has so I can't provide the answer (you may want to elaborate). What I can say: `A` is an alphanumeric field, and therefore will be right-filled with a space, `WS-FLD` is a numeric one so will be left-filled with zero (and `DISPLAY`ing hex null will commonly not work). – Simon Sobisch May 17 '21 at 10:55
  • @SimonSobisch, thank you for looking at the question. I have added further explanation to the question's body. Pls note that field A contains hexadecimal value X'0C' not right filled with space. – NoChance May 17 '21 at 12:24
  • 2
    @NoChance - By rule (for alphanumeric `MOVE`), `A` will contain `X'0C20'` (ASCII). – Rick Smith May 17 '21 at 12:53

1 Answers1

2

You moved a single byte to a 2 byte field so the move padded to the right with a space (per Simon).

MOVE X'0C' TO A.    // (A now contains x'0C20' which is not 12)   

You'd need to move both bytes to keep the value of the number intact.

MOVE x'000C' TO A

The program now displays 'SAME'.

Rick Smith
  • 3,962
  • 6
  • 13
  • 24
Jim Castro
  • 864
  • 5
  • 10
  • Excellent Jim. One more question please, I can create a new question for it if you wish, How to read the hex value 000C from a file and do the above move? – NoChance May 18 '21 at 08:42
  • @NoChance - It is better to ask a new question supplying a [MWE](https://stackoverflow.com/help/minimal-reproducible-example), as before. Specifically, include the `SELECT` statement and `FD` that describes the file and its record composition. The `PROCEDURE DIVISION` need not do more than `OPEN`, `READ`, and `CLOSE` the file. – Rick Smith May 18 '21 at 13:57
  • @RickSmith, I did what you have suggested - Thanks: https://stackoverflow.com/questions/67595834/how-to-convert-a-literal-to-hex-value – NoChance May 19 '21 at 01:35