Converting COMP to numeric and alphanumeric 
Author Message
 Converting COMP to numeric and alphanumeric

Hello!

I was wondering what the official steps of converting a PIC S9(4) COMP
and PIC S9(9) COMP to numeric and alpha numeric are.

Currently I am moving the PIC S9(9) COMP to PIC S9(9) and then to PIC
X(9).  I'm aware that I do lose the negative sign when I'm doing this
and that the COMP field could potentially contain a larger number than
S9(9).  However, I'm not sure if COBOL utilizes the whole byte to store
such a large number and so if I have to worry about it.

I would greatly appreciate if someone could tell me how to do the
conversion properly.  It would also be nice to know the conversion of
PIC S9(9) COMP-3 to numeric and alpha numeric.

Your help is greatly appreciated,



Tue, 20 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric


Quote:

>I would greatly appreciate if someone could tell me how to do the
>conversion properly.  It would also be nice to know the conversion of
>PIC S9(9) COMP-3 to numeric and alpha numeric.

>Your help is greatly appreciated,

AFAIK, in the environment that I am currently using (IBM OS/390/LE), a straight
move of the PIC S9(9) COMP to a PIC X... will expand the fields to display
format and produce a readable number.  The same is true for COMP-3.  I may be
wrong but in some of the most recent research efforts into other people's code
has shown that this is how some folks side-step the need to add extra
re-definitions of fields in their programs.  Personal preference would be to
have a redefine of the receiving PIC X field to receive the COMP/COMP-3 as PIC
S9(9) SIGN SEPARATE TRAILING.


Tue, 20 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric

Quote:

> I was wondering what the official steps of converting a PIC S9(4) COMP
> and PIC S9(9) COMP to numeric and alpha numeric are.

> Currently I am moving the PIC S9(9) COMP to PIC S9(9) and then to PIC
> X(9).  I'm aware that I do lose the negative sign when I'm doing this
> and that the COMP field could potentially contain a larger number than
> S9(9).  However, I'm not sure if COBOL utilizes the whole byte to store
> such a large number and so if I have to worry about it.

> I would greatly appreciate if someone could tell me how to do the
> conversion properly.  It would also be nice to know the conversion of
> PIC S9(9) COMP-3 to numeric and alpha numeric.

> Your help is greatly appreciated,


Is this homework ?

You move a PIC S9(9) COMP into a larger numeric field, or
even an edited numeric:

            01  Floatie-Signed      PIC -(11)9.

            MOVE Comp-Number    TO Floatie-Signed



Wed, 21 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric

Quote:

>I was wondering what the official steps of converting a PIC S9(4) COMP
>and PIC S9(9) COMP to numeric and alpha numeric are.

>Currently I am moving the PIC S9(9) COMP to PIC S9(9) and then to PIC
>X(9).  I'm aware that I do lose the negative sign when I'm doing this
>and that the COMP field could potentially contain a larger number than
>S9(9).  However, I'm not sure if COBOL utilizes the whole byte to store
>such a large number and so if I have to worry about it.

>I would greatly appreciate if someone could tell me how to do the
>conversion properly.  It would also be nice to know the conversion of
>PIC S9(9) COMP-3 to numeric and alpha numeric.

If I understand your post, you seem to be having a little difficulty
with some of the concepts here.

You don't "convert COMP to numeric", because COMP is a subset of numeric.
A numeric item is one on which you can perform numeric operations, ADD,
SUBTRACT, COMPUTE, etc.  A COMP item (or COMP-3, etc.) is a numeric item
which is formatted in some specific way, determined by the hardware
platform and compiler.  Numeric fields which are *not* defined as one of
the COMPUTATIONAL forms is inherently DISPLAY, meaning the digits could
be printed or displayed, because each digit is stored as a byte.  For all
numeric fields in COBOL, to 'convert' one type to another, you simply
move the data from the field defined one way to a field defined the other
way, and COBOL will do the necessary conversion.

An 'alphanumeric' field is simply a generic DISPLAY type field which has
no other attributes associated with it.

To convert a field defined as

  03  FIELD1      PIC S9(9) COMP (or COMP-3, or COMP-anything).

to DISPLAY, do this:

  03  FIELD2-ALPHA.
      05  FIELD2      PIC S9(9).

or

  03  FIELD2        PIC S9(9).
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(9).

  MOVE FIELD1 TO FIELD2.

But the digit which has the sign would print as an alphabetic character,
not a numeric digit.  The only difference between the two is that the group
item above will not edit properly if moved to an alphanumeric edited field.
To get around that, you can use either of these approaches:

  03  FIELD2-ALPHA.
      05  FIELD2      PIC S9(9) SIGN [LEADING / TRAILING] SEPARATE.
or

  03  FIELD2        PIC S9(9) SIGN [LEADING / TRAILING] SEPARATE.
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

Either of these would give you a + or - for the sign in a separate character,
the total field length would be 10.  But the usually preferred method is to
simply use a numeric edited field:

  03  FIELD2      PIC -9(9).
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

  03  FIELD2      PIC 9(9)-.
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

  03  FIELD2      PIC 9(9)+.
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

  03  FIELD2      PIC -(10).
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

  03  FIELD2      PIC ----,---,---.
  03  FIELD2-ALPHA  REDEFINES FIELD-2 PIC  X(10).

or any of a multitude of possibilities.
--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."



Wed, 21 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric

Quote:


> > I was wondering what the official steps of converting a PIC S9(4) COMP
> > and PIC S9(9) COMP to numeric and alpha numeric are.

> > Currently I am moving the PIC S9(9) COMP to PIC S9(9) and then to PIC
> > X(9).  I'm aware that I do lose the negative sign when I'm doing this
> > and that the COMP field could potentially contain a larger number than
> > S9(9).  However, I'm not sure if COBOL utilizes the whole byte to store
> > such a large number and so if I have to worry about it.

> > I would greatly appreciate if someone could tell me how to do the
> > conversion properly.  It would also be nice to know the conversion of
> > PIC S9(9) COMP-3 to numeric and alpha numeric.

> > Your help is greatly appreciated,

> Is this homework ?

No, I never got a formal explanation of this subject in school or
anywhere else.  In fact in my COBOL class I never heard about COMP and
COMP-3.  Now that I am out of school and writing code that should be of
good quality I think that I should know how to handle this case
properly.  Unfortunately  the manuals that I have available do not
contain much information about this subject.

BTW the .edu extension of my e-mail address does not indicate that I am
a student; it indicates that I am employed by the university.  If you
need proof look at http://www.adp.iastate.edu/staff.html.

*shakes his head about all the unfriendliness in this newsgroup toward
students*

Quote:
> You move a PIC S9(9) COMP into a larger numeric field, or
> even an edited numeric:

>             01  Floatie-Signed      PIC -(11)9.

>             MOVE Comp-Number    TO Floatie-Signed

I was wondering why the numeric field in which you move the PIC S9(9)
COMP has to be larger.  I tried on the IBM MVS system to move a number
that has more than 9 digits into the PIC S9(9) COMP field but it would
not let me do this.  I know that in 8 bytes you can store numbers with
more than 9 digits, but there is no possibility that this happens when
only COBOL is being used (or am I missing something).

This weekend I dialed-in and tried some of the suggestions that have
been posted here as well as other possibilities.  I decided to do the
following in the future when moving a computational field to an
alphanumeric field:
      01  WS-WORKING-STORAGE.
          12  WS-COMPUTATIONAL               PIC S9(9) VALUE +31 COMP.
          12  WS-NUMERIC-EDITED              PIC -9(9) VALUE +0.
          12  WS-NUMERIC-EDITED-REDEFINED
                 REDEFINES WS-NUMERIC-EDITED PIC X(10).
          12  WS-ALPHANUMERIC                PIC X(10) VALUE +0.

      MOVE WS-COMPUTATIONAL                  TO WS-NUMERIC-EDITED
      MOVE WS-NUMERIC-EDITED-REDEFINED       TO WS-ALPHANUMERIC
I choose not to use the "SIGN [LEADING / TRAILING] SEPARATE" since I did
not see any code at work that used this statement, in the hope that the
next person can maintain my code easier.

I still have not found a simple way to move an alphanumeric field to a
computational field.  Here the problem is to move an alphanumeric field
to a signed numeric field.  I probably just end up using the routine at
work that is used to edit numeric values that the user enters to an
alphanumeric field since the output of the routine is signed numeric.

Thank you to everybody that replied. I appreciate it a lot.
Do not forget that the students of today maintain your code of tomorrow
and somehow they, too, have to learn all this.



Thu, 22 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric

Quote:

>I still have not found a simple way to move an alphanumeric field to a
>computational field.  Here the problem is to move an alphanumeric field
>to a signed numeric field.

If you know the exact format of the number, you can redefine the field
using that PIC and move from that field to a numeric field.

If you know the number is a valid format, you can use FUNCTION NUMVAL or
FUNCTION NUMVAL-C to extract it.  Be careful with non-integers, because
the returned format is floating point, and on machines which use binary
floating point, you can lose precision through binary/decimal rounding.
Also, if the number is invalid, the program can abort.

If you want a COBOL routine to extract a free form number from an
alphanumeric field, returning an error flag if the number is not valid,
download GETNUM.ZIP under 'COBOL Programs' from my web page below.
--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."



Fri, 23 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric

Quote:



> >I would greatly appreciate if someone could tell me how to do the
> >conversion properly.  It would also be nice to know the conversion of
> >PIC S9(9) COMP-3 to numeric and alpha numeric.

> >Your help is greatly appreciated,

> AFAIK, in the environment that I am currently using (IBM OS/390/LE), a straight
> move of the PIC S9(9) COMP to a PIC X... will expand the fields to display
> format and produce a readable number.  The same is true for COMP-3.  I may

The COMP to PIC X won't work.  It will not be converted.  Move it to
PIC 9 redefined as Pic X or as a subordinate element to a group field
and it will work.


Fri, 23 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric
Quote:


>>I still have not found a simple way to move an alphanumeric field to a
>>computational field.  Here the problem is to move an alphanumeric field
>>to a signed numeric field.

>If you know the exact format of the number, you can redefine the field
>using that PIC and move from that field to a numeric field.

>If you know the number is a valid format, you can use FUNCTION NUMVAL or
>FUNCTION NUMVAL-C to extract it.  Be careful with non-integers, because
>the returned format is floating point, and on machines which use binary

                     --
Correction: that should be *may* be floating point (e.g. Micro Focus).
Thanks to Thane Hubbell for bringing it to my attention. :-)

Quote:
>floating point, you can lose precision through binary/decimal rounding.
>Also, if the number is invalid, the program can abort.

>If you want a COBOL routine to extract a free form number from an
>alphanumeric field, returning an error flag if the number is not valid,
>download GETNUM.ZIP under 'COBOL Programs' from my web page below.

--

Sun Valley Systems    http://personal.bhm.bellsouth.net/~judmc
"For God so loved the world that He gave His only begotten Son, that
whoever believes in Him should not perish but have everlasting life."


Fri, 23 Mar 2001 03:00:00 GMT  
 Converting COMP to numeric and alphanumeric


Quote:

> In fact in my COBOL class I never heard about COMP and COMP-3.

This is bad news.

It appears that some academics can't bring themselves to teach CoBOL even
on the rare occasions when their department says they shall.

The difference between internal and external representation is important
in most computer languages, and CoBOL has an unusually versatile set of
external representations.

[...]

Quote:
> I was wondering why the numeric field in which you move the PIC S9(9)
> COMP has to be larger.  I tried on the IBM MVS system to move a number
> that has more than 9 digits into the PIC S9(9) COMP field but it would
> not let me do this.  I know that in 8 bytes you can store numbers with
> more than 9 digits, but there is no possibility that this happens when
> only COBOL is being used (or am I missing something).

I'd say you're missing the course entitled "Introduction to Computing",
or some such, which should have been a prerequisite to the CoBOL course.
If you expect to get a semester's worth of information from such a
limited medium as Usenet, you are expecting too much.

All computer languages define an abstract machine, which the compiler
then implements with the facilities of the real machine you are using.

The mainframe on which you are working has built-in fixed-point
two's-complement binary types: a two-byte "halfword" that can hold
-32,768 to 32,767, a four-byte "fullword" that can hold -2,147,483,648 to
+2,147,483,647, and an 8-byte "doubleword" that can hold
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

When you say PIC S9(4) COMP, you are asking the compiler to set aside a
halfword.  When you say PIC S9(9) COMP, you are asking the compiler to
set aside a fullword. When you say PIC S9(18) COMP, you are asking the
compiler to set aside a doubleword.

It may seem odd at first that PIC S9(5) COMP is a fullword, not a
halfword.  After all, the five-digit number 12345 can fit in a halfword.
But, not _all_ five-digit numbers can fit in a halfword.

If the compiler set aside a halfword for PIC S9(5) COMP, sure as shooting
some wise guy would try to store 54321 in it, which wouldn't fit.  In
decimal, only -9999 to +9999 can be _guaranteed_ to fit.  -32,768 to
-10,000 and 10,000 to 32,767 are wasted.  Likewise, in decimal, only
-999,999,999 to +999,999,999 can be _guaranteed_ to fit in a fullword.
-2,147,483,648 to -1,000,000,000 and 1,000,000,000 to 2,147,483,647 are
wasted.  This is an important thing to know about internal vs. external
representations.

Although mainframe hardware can handle 31 decimal digits, CoBOL has an
overall limit of 18 digits, because IBM was outvoted at the time.
Therefore, it is relatively easy to use all five digits in a halfword
when you need them, and all ten digits in a fullword, but displaying the
nin{*filter*}th digit of a doubleword is difficult in CoBOL.

Obviously, the CoBOL abstract machine lets you specify other than PIC
S9(4) COMP, PIC S9(9) COMP, and PIC S9(18) COMP, but these are the only
real ones on the mainframe, and all the others must map to them.  The
others are of little use on the mainframe without the TRUNC option, which
will kill you.

It would be nice if PIC S9(2) COMP mapped to a byte, which it could do,
since -99 to +99 will fit quite nicely.  However, the mainframe does not
have built-in byte arithmetic, and IBM was unwilling to do the slight
extra work of simulating byte arithmetic, a reluctance that has caused
the waste of thousands upon thousands of hours over the years.

I'll let someone else do COMP-3 this time.

[...]

Quote:
>      01  WS-WORKING-STORAGE.
>          12  WS-COMPUTATIONAL               PIC S9(9) VALUE +31 COMP.
>          12  WS-NUMERIC-EDITED              PIC -9(9) VALUE +0.
>          12  WS-NUMERIC-EDITED-REDEFINED
>                 REDEFINES WS-NUMERIC-EDITED PIC X(10).
>          12  WS-ALPHANUMERIC                PIC X(10) VALUE +0.

>      MOVE WS-COMPUTATIONAL                  TO WS-NUMERIC-EDITED
>      MOVE WS-NUMERIC-EDITED-REDEFINED       TO WS-ALPHANUMERIC

I'm sure that, one-on-one, your question could be answered in a short
time, but in this medium, unfortunately, your example is as puzzling as
your original article.  Leaving aside the minor errors, it's not clear
why you think you need to redefine PIC -9(9) as X(10), and if for some
reason you did, it would be better to let it flow upward than to
explicitly redefine.  The second MOVE, from PIC X(10) to PIC X(10), has
no purpose that I can see.  I can only repeat the question others have
asked, namely, why do you think you need to do this?

[...]

Quote:
> I still have not found a simple way to move an alphanumeric field to a
> computational field.

You don't need to do this, either.  All computational input to CoBOL
programs is in numeric fields on punched cards.  :-)

--
Christopher Westbury, Midtown Associates, 15 Fallon Place, Cambridge, MA 02138



Sat, 24 Mar 2001 03:00:00 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. problem in alphanumeric to numeric move........

2. CONVERSION NUMERIC->ALPHANUMERIC

3. Is there a util to convert comp fields to non-comp

4. Converting hex string values to numeric

5. Convert 1D string Array to Numeric Array

6. Converting array numeric precision?

7. Help! Converting Numeric to String

8. converting chars to numeric

9. converting numeric value to ascii (beginner)

10. convert character string to numeric value

11. Internal Files: convert numeric value to character form

12. Convert hex data to numeric format

 

 
Powered by phpBB® Forum Software