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

Packed Hexadecimal addition


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

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Mon Apr 06, 2009 11:09 pm
Reply with quote

Hi

I have a VSAM KSDS file with a packed hexadecimal three digit number as a part of its key like

012
01B

I wish to carry out basic addition on this so that I can get the value like

012
01C

012
01D

and so on and so forth..

Any idea how this can be done in COBOL ??
Back to top
View user's profile Send private message
Douglas Wilder

Active User


Joined: 28 Nov 2006
Posts: 305
Location: Deerfield IL

PostPosted: Mon Apr 06, 2009 11:28 pm
Reply with quote

Packed number are defined as comp-3.
For Example:
Code:
    10  PACK-NUM1      PIC S9(03) COMP-3 VALUE ZERO.

Then do your math:
Code:
        COMPUTE PACK-NUM1 = PACK-NUM + 1.

COBOL knows how to handle the math and signs in packed numbers.
Back to top
View user's profile Send private message
himanshupant

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Mon Apr 06, 2009 11:34 pm
Reply with quote

Its not a packed COMP number .. Its a hexadecimal number like say

x'00112A'

and when I repeatedly add x'000001' to it

I want

x'00112B'

x'00112C'

.
.
.
.
x'00112F'
x'001130'

etc
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Mon Apr 06, 2009 11:44 pm
Reply with quote

Code:
    10  PACK-NUM1      PIC S9(06) COMP VALUE ZERO.

Then do your math:
Code:
        COMPUTE PACK-NUM1 = PACK-NUM + 1.
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: Mon Apr 06, 2009 11:48 pm
Reply with quote

Hello,

Quote:
I have a VSAM KSDS file with a packed hexadecimal three digit number as a part of its key like
Quote:
Its not a packed COMP number .. Its a hexadecimal number like say
Your terminology is completely confusing - because you are not using standard terms. You need to learn the proper names for things. . .

For starters - every byte on the mainframe is a hexadecimal value - x'00' thru x'FF'. Every byte is also a binary value - b'0000 0000' thru b'1111 1111'.

What it the picture of the 3-byte field in the vsam file?

One way to do what you say you want is to define an s9(8) comp field, redefine it as a 1 byte filler and a 3-byte pic xxx field. To increase the value by one, simply add 1 to the s9(8) comp field and move the pic xxx fiedl to whereever you want it.
Back to top
View user's profile Send private message
himanshupant

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Mon Apr 06, 2009 11:51 pm
Reply with quote

sorry about the inadvertent confusion..


the three byte field`s PIC clause is XXX.


So if I add a simple 1 to the XXX will I get the desired result ??
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: Mon Apr 06, 2009 11:55 pm
Reply with quote

Hello,

No.

You should do as suggested and define an s9(8) comp field and redefine it. As your data of interest is 3 bytes, make the first byte of the redefinition a filler and bytes 2-4 your 3-byte value. Add to the comp field to increment the xxx value.
Back to top
View user's profile Send private message
mmwife

Super Moderator


Joined: 30 May 2003
Posts: 1592

PostPosted: Tue Apr 07, 2009 4:05 am
Reply with quote

If you're confined to a 3 byte field because the file is already in use with that configuration, you can move the 3 bytes to a four byte COMP field (redefine the COMP field to PIC X(4), VALUE the COMP field to zeros and use ref/mod to move the 3 byte VSAM key field to it [e.g. move 3-BYTES to PIC-X-4-BYTES(2:3) ].

After the math, ref/mod move the the PIC4 field back to the VSAM key field.
Back to top
View user's profile Send private message
himanshupant

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Tue Apr 07, 2009 10:42 am
Reply with quote

I am using something like the below

03 WS-HEX-NUMBER-NUM PIC S9(8) COMP VALUE X'00110A'.
03 WS-HEX-NUMBER REDEFINES WS-HEX-NUMBER-NUM.
05 FILLER PIC X.
05 WS-HEX-CHAR PIC XXX.
.
.
.
.
.
PROCEDURE DIVISON
.
.
.
.
.
ADD 1 TO WS-HEX-NUMBER-NUM
DISPLAY ' HEX 2 = ' WS-HEX-CHAR



I am getting a compilation error as below


IGYGR1080-S A "VALUE" clause literal was not compatible with the data category of subject data item. The "VALUE" clause was discarded.

icon_sad.gif
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Tue Apr 07, 2009 11:08 am
Reply with quote

Try:
Code:
03  WS-HEX-NUMBER-NUM   PIC S9(8) COMP VALUE 4362.
03  WS-HEX-NUMBER       REDEFINES WS-HEX-NUMBER-NUM.   
    05  FILLER          PIC X.                         
    05  WS-HEX-CHAR     PIC XXX.                       
Or:
Code:
03  WS-HEX-CHARACTER    PIC X(4) VALUE X'00110A'.
03  WS-HEX-NUMBER-NUM   REDEFINES WS-HEX-CHARACTER
                        PIC S9(8) COMP.
03  WS-HEX-NUMBER       REDEFINES WS-HEX-NUMBER-NUM.   
    05  FILLER          PIC X.                         
    05  WS-HEX-CHAR     PIC XXX.
Back to top
View user's profile Send private message
himanshupant

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Tue Apr 07, 2009 11:28 am
Reply with quote

thanks... seems to work..

One more question , any way I can see the number in SYSOUT of the job

for example if I add 1 to 00110A and I get 0011OB , can I see this value in SYSOUT ?

I am not able to see it if I am doing a display like

DISPLAY WS-HEX-CHAR
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Tue Apr 07, 2009 11:39 am
Reply with quote

Since a binary X'00110A' is a decimal '4362', which are you trying to 'see'?
If WS-HEX-CHAR is comp, COBOL will display the latter, if WS-HEX-CHAR is character, COBOL will display the three bytes but you will need HEX ON to see it.
There have been several good snipits of COBOL code given to display hex character strings as display, ie., X'00110A' displayed as X'F0F0F1F1F0C1'.
Back to top
View user's profile Send private message
himanshupant

New User


Joined: 21 Mar 2007
Posts: 46
Location: India

PostPosted: Tue Apr 07, 2009 11:53 am
Reply with quote

I am trying to see X'00110A ' as character 00110A

WS-HEX-CHAR is comp but what I am seeing on adding 1 to it in hex format is x'110A41' ... this is pretty strange ..

could it be that the string has encountered some conversion like into 2s complement form or something like that ??
Back to top
View user's profile Send private message
CICS Guy

Senior Member


Joined: 18 Jul 2007
Posts: 2146
Location: At my coffee table

PostPosted: Tue Apr 07, 2009 12:05 pm
Reply with quote

As far as the x'110A41' goes, I'd guess that you used the second example and did not correct my error:
03 WS-HEX-CHARACTER PIC X(4) VALUE X'00110A'.
should be:
03 WS-HEX-CHARACTER PIC X(4) VALUE X'0000110A'.
icon_redface.gif
And again I repeat:
Quote:
There have been several good snipits of COBOL code given to display hex character strings as display, ie., X'00110A' displayed as X'F0F0F1F1F0C1'.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Tue Apr 07, 2009 4:44 pm
Reply with quote

Quote:
I am trying to see X'00110A ' as character 00110A

WS-HEX-CHAR is comp but what I am seeing on adding 1 to it in hex format is x'110A41' ... this is pretty strange ..

could it be that the string has encountered some conversion like into 2s complement form or something like that ??
Actually, COBOL did exactly what you told it to do. PIC X(04) field with value X'00110A' has the value left-justified blank filled, so the actual value in the field is X'00110A40'. Add 1 to that and you get X'00110A41'.
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 PD not working for unsigned packed JO... DFSORT/ICETOOL 5
No new posts Def PD not working for unsigned packe... JCL & VSAM 3
No new posts Need Help with Packed Decimal Signs DFSORT/ICETOOL 4
No new posts Select a DB2 value in a specific deci... DB2 4
No new posts Numeric check on packed signed and un... COBOL Programming 4
Search our Forums:

Back to Top