converting string to pack decimal form

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

converting string to pack decimal form

Postby mayur475 » Wed Jun 29, 2016 10:55 pm


I want to convert numeric string ('12345 ' 10 byte) into packed decimal format in one cobol program, and in another cobol program same packed decimal value converted back to string ('12345 ' 10 byte)

I tried with below logic

Converting string to packed:-
Moved '12345 ' to numeric field 9(09)
move numeric field 9(09) to packed field S9(10) COMP-3
packed field = packed field * 10
Move packed field to X(05) (here I got value as x'0123450000')

Converting packed to string:-
Move X(05) to packed field S9(10) COMP-3
packed field = packed field / 10
Move packed field to numeric field 9(09) (here I got value as ' 123450000') <---- here I want value as '12345 ' but not working

Can anyone please tell me logic for this?
Posts: 1
Joined: Wed Jun 29, 2016 10:10 pm
Has thanked: 0 time
Been thanked: 0 time

Re: converting string to pack decimal form

Postby Robert Sample » Thu Jun 30, 2016 12:18 am

The first issue is that COBOL has variables, not strings. A "string" is a specific type of data, and COBOL doesn't support strings.

The second issue is that COBOL is doing precisely what you told it to do. You apparently don't understand alignment rules for alphanumeric and numeric variables in COBOL. I recommend you read the Enterprise COBOL Language Reference manual on the different types of variables.

You probably can use intrinsic function NUMVAL to get the results you want, depending upon which version of COBOL you are using. The intrinsic functions are explained in the Language Reference manual as well. If NUMVAL won't do it what you need, you'll probably have to use reference modification or a REDEFINES into an array and handle each byte separately.
Robert Sample
Global moderator
Posts: 3656
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 270 times

Re: converting string to pack decimal form

Postby Terry Heinze » Thu Jun 30, 2016 7:05 pm

After studying the manual as Robert suggests, if you still don't get the result you want, redefine your intermediate fields as alphanumeric (PIC X) and display each after every operation. Display your SYSOUT in hex and study those values. Also, why are you multiplying by 10? Also, PIC S9(10) COMP-3 is stored in 6 bytes, not 5. Let us know the result.
.... Terry
Terry Heinze
Posts: 232
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 11 times
Been thanked: 11 times

Return to IBM Cobol


  • Related topics
    Last post