Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Ascii char to Decimal value Conversion

467 views
Skip to first unread message

Rupesh Sonawane

unread,
Jan 11, 2022, 11:13:15 AM1/11/22
to
How can we convert Ascii string char to its decimal value?

Rupesh Sonawane

unread,
Jan 11, 2022, 11:14:19 AM1/11/22
to
How can we convert Ascii string char to its decimal value in Cobol or Screen Cobol requester?

Bill Honaker

unread,
Jan 11, 2022, 1:15:31 PM1/11/22
to
On Tue, 11 Jan 2022 08:14:18 -0800 (PST), Rupesh Sonawane <rupso...@gmail.com> wrote:

> How can we convert Ascii string char to its decimal value in Cobol or Screen Cobol requester?

Rupesh,

In most COBOL languages this is done with a REDEFINES structure. For example:

01 CHAR-TEST-INT PIC 9(4) COMP.
01 CHAR-TEST-CHAR REDEFINES CHAR-TEST-INT.
02 FILLER PIC X.
02 CHAR-TEST-CHAR-VAL PIC X.

...

MOVE ZERO TO CHAR-TEST-INT.
MOVE CHAR-TO-TEST to CHAR-TEST-CHAR-VAL.

...

After the above two statements, the CHAR-TEST-INT variable will contain the decimal value you need.

Disclaimer - this example as entered from memory and may contain syntax errors. It's meant as a description of the solution only.
Bill

Keith Dick

unread,
Jan 11, 2022, 2:42:35 PM1/11/22
to
The REDEFINES idea is a valid way to approach the task, but you got a lot of the details wrong. Don't feel bad. I imagine it has been a long time since you have done any COBOL programming!

I think what would work a little better (but is not perfect) would be:

01 ASCII-TEXT PIC X(5).
01 DISPLAY-NUMERIC REDEFINES ASCII-TEXT PIC 9(5).

01 INPUT-TEXT PIC X(5).
01 BINARY-VALUE PIC 9(5) COMP.

MOVE INPUT-TEXT TO ASCII-TEXT.
MOVE DISPLAY-NUMERIC TO BINARY-VALUE.

The above would only work if INPUT-TEXT contained 5 digit characters -- no leading, trailing, or embedded spaces, no sign. With further programming, those limitations can be addressed, but I'm not going to try to do that right now.

For COBOL, not Screen COBOL, it would be possible to call a system procedure that scans an ASCII string that contains a representation of a numeric value and returns a binary value. That would have a lot fewer restrictions that the simple code above has. If I were doing the programming, that probably is the way I would go, unless it was important that the COBOL program be portable to other operating systems.

Bill Honaker

unread,
Jan 11, 2022, 3:23:48 PM1/11/22
to
Keith,

I understood the question differently than you. I thought he was looking to find the integral equivalent of the ASCII character. That is, a space would be equal to 32.
This would be equivalent to the Basic (or Visuaal Basic) chr() function.

If the OP intended to convert a sequence of numeric characters to a comp field, then your solution is correct.

Bill

Keith Dick

unread,
Jan 11, 2022, 3:37:35 PM1/11/22
to
Bill,

Ah, I see now. Yes, the original question could be interpreted either way. That's a frequent problem with remote help like this. I wonder which interpretation is the one the original poster had in mind.

Bill Honaker

unread,
Jan 11, 2022, 6:04:29 PM1/11/22
to
Keith,

Hopefully the OP will reply to us. Rupesh, please clarify and let us know if your question got answered.
Bill

Rupesh Sonawane

unread,
Jan 12, 2022, 2:40:54 AM1/12/22
to
Thanks for your response.
Yes Bill, Keith,
I was looking to find the integral equivalent of the ASCII character. That is, a space would be equal to 32.
So will it work with REDEFINES as mentioned earlier?
I have 7 alphanumeric chars strings and need to convert in Numeric data so I was referring to convert it to the equivalent decimal value of ASCII char.

Keith Dick

unread,
Jan 12, 2022, 5:26:15 AM1/12/22
to
Yes, the code that Bill posted will get the value of the ASCII code for a single character.

You said you need to get the value of each character of a seven-character string. If you don't already have each character in a separate data field, you might need to use REDEFINES and OCCURS to let you pick out each character individually.

Something like this, I believe:

01 SEVEN-CHAR-FIELD PIC X(7).
01 ARRAY-GROUP REDEFINES SEVEN-CHAR-FIELD..
02 ONE-CHAR PIC X OCCURS 7 TIMES.
01 I PIC 9.

PERFORM 200-GET-ONE-CHAR-VALUE VARYING I FROM 1 TO 7.

...

200-GET-ONE-CHAR-VALUE.
MOVE ZERO TO CHAR-TEST-INT.
MOVE ONE-CHAR (I) TO CHAR-TEST-CHAR-VAL.
DISPLAY CHAR-TEST-INT.

The fields I didn't declare here are from Bill's code. This code just displays the decimal code value of each character. You'll probably want to do something besides that with those values.

I never remember the rules about using OCCURS and REDEFINES together, and I always need to check the manual. I think the rule is that you cannot use both REDEFINES and OCCURS on the same data item. That is why I nested the OCCUR under the item that uses REDEFINES. But I am not sure that is the rule, and I did not check the manual before writing this suggested code, so I might have gotten the syntax wrong for putting an array of one-character fields over the seven-character field. Check the manual before believing what I wrote is exactly correct.

I believe Screen COBOL is somewhat limited in this kind of manipulation, so this suggested code might only work in ordinary COBOL, not in Screen COBOL. Right now, I don't remember what limitation of Screen COBOL would keep this from working (and maybe I'm mistaken about it not working in Screen COBOL).

Good luck!
0 new messages