Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple Data conversion from char to hex prob

Status
Not open for further replies.

bmcl188

Programmer
Feb 23, 2005
2
GB
I am being stupid here but I have a 6 byte pic x or pic 9 field that I need to get into a Pic s9(9) field, but can't get the results to work

hate cobol, easier in assembler!
 
03 WS-START-DATE. pic x(6)

03 WS-END-DATE-R PIC S9(9) COMP

juts moving top field to bottom. value in pic x field is 050222, expecting C42E in receiving field (which is then used in a db2 select) but don't seem to be getting it.
if i display pic s9(9)field after move it is 25231745M




 
bmcl: COMP fields store the value in binary notation. A display won't necessarily get what you are looking for. If you want to see what you have in the field in human terms, move the S9(9) COMP to a +(8)9 field so you can display it along with the sign (which the S field indicates).

But to be sure I would do this:

Code:
03 start-date pic x(6).
03 start-num redefines start-date pic 9(6).
03 end-date-r pic s9(9) COMP.

MOVE START-NUM TO END-DATE-R.

Moving text data (X) to a number inevitably can cause problems. My thought is to always be sure of what you are doing.
 
And this ?
COMPUTE WS-END-DATE-R = FUNCTION NUMVAL(WS-START-DATE)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
If I understand correctly you are expecting
WS-ENd-DATE-R to have the value 50222 on it.

So assuming that WS-START-DATE is always numbers you can also do the following.
move WS-START-DATE(1:) to WS-END-DATE-R
or


03 WS-START-DATE. pic x(6)
03 WS-R REDEFINES WS-START-DATE PIC 9(6).

and then
move WS-R to WS-END-DATE-R

for you to verify that the move was sucessfull do as

03 ws-display pic -9(9) sign leading separate.

move WS-END-DATE-R to ws-display
display "date = " ws-display.



Regards

Frederico Fonseca
SysSoft Integrated Ltd
 
This is what you said was happening:


03 WS-START-DATE. pic x(6)

03 WS-END-DATE-R PIC S9(9) COMP

juts moving top field to bottom. value in pic x field is 050222, expecting C42E in receiving field (which is then used in a db2 select) but don't seem to be getting it.
if i display pic s9(9)field after move it is 25231745M


I noticed that you have a period after WS-START-DATE. I suspect that is really a group item. I ran some code on an IBM using Enterprise COBOL and here is what happens.

When you move a group item to a binary item, it does an assembler MVC (Move character). Since the binary item is 4 bytes long, it moves 4 bytes. The character move will move data from left to right. The leftmost 4 bytes of your sending field are 0502 which in hex is F0 F5 F0 F2. This is the value you have in your binary field. Remember, a binary item is negative if the left-most bit is on, so this appears to be a negative number.

Binary equivalent of F0 F5 F0 F2 is:
1111 0000 1111 0101 1111 0000 1111 0010

To reverse the twos complement form, subtract 1:

1111 0000 1111 0101 1111 0000 1111 0001

Then reverse the bits:
0000 1111 0000 1010 0000 1111 0000 1110

In hex that is:
0F 0A 0F 0E

The decimal equivalent is 252,317,454. Remember, this was a negative number. A negative 4 is hex D4 which is the letter M.

The DISPLAY statement converts the binary value to its decimal equivalent but does not edit the sign. So you saw 25231745M as your result.

Bottom line:
If you move an elementary numeric item (PIC 9(06) ) or use reference modification as suggested earlier, the assembler code generated first packs your number and then does a CVB to convert it to binary. You get the proper results in that case.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top