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

Converting from X(5) to 9(2) COMP variable


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

Active User


Joined: 19 Mar 2009
Posts: 206
Location: Globe, India

PostPosted: Wed Mar 06, 2013 11:55 pm
Reply with quote

I am in a such a requirement where i have to convert a data from X(5) type to 9(2) COMP.
My source string will be always of below type with X(5):
1 character in the beginning and rest 4 are numbers.
e.g A1152, U1153, C0621 etc...

I understand that data with a character can not be converted into COMP field.
when i am passing U1153 to 9(2) COMP field, i am getting data as '41153' which is quiet obvious.

Is there any way to convert X(5) to 9(2) COMP for handling above situation?
I need to find some way as i have to write data to journal in 9(2) COMP format. Existing logic will decode it to displayable format further.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Thu Mar 07, 2013 12:07 am
Reply with quote

the existing logic is ( using a very kind adjective) flawed
the algorithm is not reversible ...

as You can see from this hex display

Code:
 EDIT       ENRICO.CUST.JCL(AAA) - 01.00                    Columns 00001 00072
 Command ===>                                                  Scroll ===> CSR
 ****** ***************************** Top of Data ******************************
 000001 {ABCDEFGHI­ôöòóõ
        CCCCCCCCCCCCCCCC44444444444444444444444444444444444444444444444444444444
        0123456789ABCDEF00000000000000000000000000000000000000000000000000000000
 ------------------------------------------------------------------------------
 000002 }JKLMNOPQR¹ûüùúÿ
        DDDDDDDDDDDDDDDD44444444444444444444444444444444444444444444444444444444
        0123456789ABCDEF00000000000000000000000000000000000000000000000000000000
 ------------------------------------------------------------------------------
 000003 \÷STUVWXYZ²ÔÖÒÓÕ
        EEEEEEEEEEEEEEEE44444444444444444444444444444444444444444444444444444444
        0123456789ABCDEF00000000000000000000000000000000000000000000000000000000
 ------------------------------------------------------------------------------
 ****** **************************** Bottom of Data ****************************


D, M, U, when converting from X to comp map to 4
so ... Your organization should review the logic
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Thu Mar 07, 2013 12:09 am
Reply with quote

Whether it's 9(02) COMP or 9(04) COMP, it occupies two-bytes (halfword). Why not define it as 9(04) COMP and specify the TRUNC(BIN) compiler option or better yet, define it as COMP-5 (if supported by your compiler).

An unsigned halfword (either TRUNC(BIN) with COMP/COMP-4/BINARY or COMP-5 straightaway), has a maximum capacity of 65535 decimal (X'FFFF'). So, regardless of the definition used (and based upon your example data), you WILL have high-order truncation issues as well as other "opportunities".

You may have to get creative.... icon_wink.gif
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


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

PostPosted: Thu Mar 07, 2013 12:10 am
Reply with quote

Unless there are rules that you haven't explained, what you want to do is not physically possible. There are 26 letters A to Z and with 4 digits there are 10,000 possible values (0000 to 9999). Hence there are 260,000 possible combinations of letters and digits. A PIC 9(02) COMP variable is a binary half-word or 16 bits. There are 65,536 unique combinations of 16 bits. You cannot squeeze 260,000 unique values into 65,536 unique values without losing something somewhere.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Thu Mar 07, 2013 12:30 am
Reply with quote

Code:
01  a-nice-name COMP/BINARY/COMP-4 PIC 9(2).
01  another-nice-name COMP-5 PIC 9(2).


a-nice-name, with compile option TRUNC(OPT) or TRUNC(STD), has a maximum value of 99. The space occupied will be two bytes. With TRUNC(BIN), it behaves as does another-nice-name.

another-nice-name is as described by Mr Bill and Robert, 65,536 possibilities, or a value of zero to 65,535. As enrico has pointed out, it doesn't matter what you do, or what influence the compile options have, you are not going to be able to fully represent your input, no matter what existing code there is.
Back to top
View user's profile Send private message
Ed Goodman

Active Member


Joined: 08 Jun 2011
Posts: 556
Location: USA

PostPosted: Thu Mar 07, 2013 10:17 pm
Reply with quote

Is there a limit on the number? Like can it only go up to 3000? or do you need to allow for 9999?
Back to top
View user's profile Send private message
jerryte

Active User


Joined: 29 Oct 2010
Posts: 202
Location: Toronto, ON, Canada

PostPosted: Fri Mar 08, 2013 1:25 am
Reply with quote

If you don't want the first letter of the field then the below logic should work:

Code:
05 WS-CHAR5        PIC X(5).
05 WS-COMP2        PIC S9(4) COMP-5.
05 WS-NUM4          PIC 9(4).

MOVE WS-CHAR5 (2:) TO WS-NUM4
MOVE WS-NUM4 TO WS-COMP2
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Fri Mar 08, 2013 4:56 am
Reply with quote

Or, turning a "blind eye" to the brutal data-names:

Code:
05  WS-CHAR5        PIC X(5).
    10  WS-CHAR5 -CHAR       PIC X.
    10  WS-CHAR5 -NUM        PIC 9(4).
05 WS-COMP2        PIC S9(2) COMP. (how TS defined it).

IF WS-CHAR5 -NUM NUMERIC
    MOVE WS-CHAR5 -NUM TO WS-COMP2
ELSE
    do whatever the spec describes for this situation, if it is possible
END-IF
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 Issues Converting From ZD to Signed N... DFSORT/ICETOOL 4
No new posts Extracting Variable decimal numbers f... DFSORT/ICETOOL 17
No new posts Variable Output file name DFSORT/ICETOOL 8
No new posts COBOL - Move S9(11)v9(7) COMP-3 to -(... COBOL Programming 5
No new posts Moving Or setting POINTER to another ... COBOL Programming 2
Search our Forums:

Back to Top