how to convert string to number?



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

how to convert string to number?

Postby helen2000 » Sat Mar 13, 2010 2:57 am

Hi All,

Does anybody know how to convert a string to number in Cobol? we have 3 var as following:
03 NUM0   PIC X(6).           
 03 NUM1   PIC 9(06).           
 03 NUM2   PIC 9(06) VALUE 9754.
........................
 MOVE '9754' TO NUM0   
 MOVE NUM0 TO NUM1   
 IF NUM1 = NUM2                 
     DISPLAY "SUCCESSFUL!"     
 ELSE                           
     DISPLAY "CONVERSION FAIL!"
 END-IF                         
     
 DISPLAY 'NUM0=' NUM0   
 DISPLAY 'NUM1=' NUM1   
 DISPLAY 'NUM2= ' NUM2 

I got the output as following:
CONVERSION FAIL!                                 
NUM0=9754                                       
NUM1=9754 0                                     
NUM2= 009754                                     
 ******************************* Bottom of Data *

why is num1 "9754 0", how to convert "9754" to number 9754, thank you very much!

helen
helen2000
 
Posts: 85
Joined: Sat Aug 08, 2009 9:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: how to convert string to number?

Postby dick scherrer » Sat Mar 13, 2010 3:20 am

Hello,

MOVE '9754' TO NUM0 - this instruction caused NUM0 to contain 9754bb.

Suggest you use NUMVAL in this MOVE (MOVE NUM0 TO NUM1). Read here:
http://publibz.boulder.ibm.com/cgi-bin/ ... r50/7.1.32
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: how to convert string to number?

Postby ITConsultant » Sat Mar 13, 2010 3:22 am

Num0 is PIC X meaning alpha numeric.
Num1 & 2 are numeric.

you should read up the rules on moving alph numeric to numeric etc.

If you look at the output in HEX you will see the difference.

BOTTOM LINE - READ THE BOOK / MANUAL.
ITConsultant
 
Posts: 15
Joined: Wed Mar 10, 2010 4:28 am
Has thanked: 0 time
Been thanked: 0 time

Re: how to convert string to number?

Postby helen2000 » Sat Mar 13, 2010 4:11 am

thank you, Mr. Dick and ITConsultant.
could you tell me how to check output in HEX? I'd like to know
why num1 is "9754b0" instead of "9754bb", thanks again,
NUM0=9754                                       
NUM1=9754 0                                     
NUM2= 009754                                     
******************************* Bottom of Data *


helen
helen2000
 
Posts: 85
Joined: Sat Aug 08, 2009 9:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: how to convert string to number?

Postby dick scherrer » Sat Mar 13, 2010 4:33 am

Hello,

Because of the way COBOL moves are serviced at run time. The system tried to do what it believed you wanted. Which is why we need to be quite aware of what we have asked the system to do.

One way to see values in HEX is to view the output file in tso/ispf and then use HEX ON.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: how to convert string to number?

Postby ITConsultant » Sat Mar 13, 2010 4:34 am

Assuming you are on the mainframe & can see / access your job's sysout, just type HEX on the command line to switch to HEX mode. You can switch back by typing HEX OFF.

The "b" or space between 4 & 0 for NUM1 is probably not a space and is a low value. You can confirm it in HEX (HEX value for space would be 40 and F0 for 0. Low value in HEX is 00).

HEX mode displays three lines for every text / EBCDIC line. First line would be your text as you normally see it, next two lines are HEX. For example if in text you see HELEN2000, in HEX mode it will appear as following:

HELEN2000
CCDCDFFFF
8535520000

H = x'C8'
E = x'C5'
.... and so on.
ITConsultant
 
Posts: 15
Joined: Wed Mar 10, 2010 4:28 am
Has thanked: 0 time
Been thanked: 0 time

Re: how to convert string to number?

Postby dick scherrer » Sat Mar 13, 2010 9:54 am

Hello,

The "b" or space between 4 & 0 for NUM1 is probably not a space and is a low value.
Why would you believe this? As this is so little work to test, why not run a little experiment and be sure before posting :(

It took less than 5 minutes to copy/paste the code in question and run this. . .
CONVERSION FAIL!
CDDECDECDD4CCCD54
365559296506193A0
_________________
NUM0=9754       
DEDF7FFFF44444444
5440E975400000000
_________________
NUM1=9754 0     
DEDF7FFFF4F444444
5441E975400000000
_________________
NUM2= 009754     
DEDF74FFFFFF44444
5442E000975400000
Note that the embedded space really is a space. . .

If you look at the output in HEX you will see the difference.

BOTTOM LINE - READ THE BOOK / MANUAL.
I believe you posted a good suggestion - one that you should follow before posting guesses as solutions (i.e. review the manual and verify the results).
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: how to convert string to number?

Postby helen2000 » Sat Mar 13, 2010 10:46 am

anyway, I got a lots from both of you, thank you in advance.
but my question is:why num1 is "9754b0" instead of "9754bb"? as you said, the output produce is
at runtime, but I run the same program in different machine and move any num string "nnnn" to num0,
for num1 always got "nnnn 0" instead of "nnnnbb", I confuse here, thanks again.

helen
helen2000
 
Posts: 85
Joined: Sat Aug 08, 2009 9:44 pm
Has thanked: 0 time
Been thanked: 0 time

Re: how to convert string to number?

Postby dick scherrer » Sat Mar 13, 2010 11:24 am

Hi Helen,

but my question is:why num1 is "9754b0" instead of "9754bb"?
Because num1 is defined as numeric - 9(06). As num0 is alphanumeric the x(06) value resulting from the first move is 9754bb.

but I run the same program in different machine and move any num string "nnnn" to num0,
for num1 always got "nnnn 0" instead of "nnnnbb"
Yes, this is the expected behavior.

Suggest you change the code to use 9(08) as the picture for num1 and run the code again as a demonstration.

If you have not included the NUMVAL in a test, suggest you do that too.
Hope this helps,
d.sch.
User avatar
dick scherrer
Global moderator
 
Posts: 6268
Joined: Sat Jun 09, 2007 8:58 am
Has thanked: 3 times
Been thanked: 93 times

Re: how to convert string to number?

Postby Robert Sample » Sat Mar 13, 2010 6:56 pm

IIRC, depending upon the compiler options, COBOL will enforce signs. Which means when you move a value to a numeric field, COBOL adds an Assembler OR instruction (actually an OI or OR Immediate) to force the last byte to be F?. Since a space is X'40' and a zero is X'F0', the output of the OR instruction is a zero. So you get 4 digits, a space, and a zero in the six-character field. This is normal, expected, and typical COBOL behavior.
Robert Sample
Global moderator
 
Posts: 3720
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Next

Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post