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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conversion of numeric to alphanumeric and viceversa

Status
Not open for further replies.

kurup

Programmer
Jun 1, 2001
32
IN
Is there any function to convert alphanumeric to numeric values? I want to extract first 2 bytes frm an alphanumeric value and move that to numeric value..Sine this will cause S0C7 abend, is there any other way to do this?
 
Hi kurup,

You should be able to do this:

01 dest-field PIC 9(04) VALUE ZERO.
01 alpha-field PIC X(04) VALUE "1234".
MOVE alpha-field (1: 2) TO dest-field (3: 2).

Shouldn't you?

Dimandja
 
A 1985 standard compliant compiler should be able to do the following:
Code:
01  alpha-value   pic x(20).
01  redefines alpha-value.
    02  alpha-to-convert  pic zz.
    02  pic x(18).

01  num-value     pic 9(2).
...

    move alpha-to-convert to num-value.

The 1985 standard defines a 'deedit' facility for moving from a numeric edited field to a numeric field. Look in your compiler's documentation, probably in the MOVE statement's documentation.
Tom Morrison
 
what happens if the value of pic x(04) is " 12" and you move spaces to a numeric field???

That is why this redefine is important.
Before you use a redefined value from x to 9 make sure it acutually contains a numeric value.

One way is to use the inspect verb and replace all spaces with zeros.

Even then the field could be filled with all alphabetic characters and you should test the data.


If you do not like my post feel free to point out your opinion or my errors.
 
If your remark is directed to me --

Moving from numeric edited to numeric should be tolerant of these scenarios. In my example, using your data, spaces is a reasonable value to be contained in a PIC ZZ, so the result should be the same as move 0 to numeric.

Using INSPECT to replace all spaces with zeros would have an undesirable result when the X(4) value is, for example, "1 AB". Is the expected result 1 or 10? Depends...
Tom Morrison
 
The de-editing of DISPLAY NUMERIC values when MOVEd to NUMERIC fields was a new feature with ANSI 85 (COBOL II for you mainframers). Many old hands never received good training on many of those "new" features and may not be familiar with this "now standard" COBOL feature.

Glenn

PS, the NUMVAL intrinsic function does a better job of handling a variety of formatted, numeric input. Unfortunately, it does its work in floating point which does not always produce the exact result you expect.
 
this works

01 an-data.
05 could-be-numbers pic xx.
05 other-date pic x(10)
01 nu-data redefines an-data.
05 is-numbers pic 99.
05 filler pic x(10).

move some-characters to an-data.
if (could-be-numbers numeric)
add isnumbers to some-accumulator.
Enjoy Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top