Page 1 of 1

converting string to pack decimal form

PostPosted: Wed Jun 29, 2016 10:55 pm
by mayur475
Hi,

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?

Re: converting string to pack decimal form

PostPosted: Thu Jun 30, 2016 12:18 am
by Robert Sample
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.

Re: converting string to pack decimal form

PostPosted: Thu Jun 30, 2016 7:05 pm
by Terry Heinze
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.