• Re: Ascii char to Decimal value Conversion in Cobol or Screen Cobol

    From Rupesh Sonawane@21:1/5 to All on Tue Jan 11 08:14:18 2022
    How can we convert Ascii string char to its decimal value in Cobol or Screen Cobol requester?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bill Honaker@21:1/5 to Rupesh Sonawane on Tue Jan 11 12:15:29 2022
    On Tue, 11 Jan 2022 08:14:18 -0800 (PST), Rupesh Sonawane <rupsonawane@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Dick@21:1/5 to Bill Honaker on Tue Jan 11 11:42:34 2022
    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote:
    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

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bill Honaker@21:1/5 to Keith Dick on Tue Jan 11 14:23:46 2022
    On Tue, 11 Jan 2022 11:42:34 -0800 (PST), Keith Dick <rkdick@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote:
    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

    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.


    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Dick@21:1/5 to Bill Honaker on Tue Jan 11 12:37:34 2022
    On Tuesday, January 11, 2022 at 12:23:48 PM UTC-8, Bill Honaker wrote:
    On Tue, 11 Jan 2022 11:42:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote:
    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

    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.
    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

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Bill Honaker@21:1/5 to Keith Dick on Tue Jan 11 17:04:27 2022
    On Tue, 11 Jan 2022 12:37:34 -0800 (PST), Keith Dick <rkdick@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 12:23:48 PM UTC-8, Bill Honaker wrote:
    On Tue, 11 Jan 2022 11:42:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote:
    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

    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.
    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

    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.

    Keith,

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rupesh Sonawane@21:1/5 to Bill Honaker on Tue Jan 11 23:40:53 2022
    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.


    On Wednesday, January 12, 2022 at 4:34:29 AM UTC+5:30, Bill Honaker wrote:
    On Tue, 11 Jan 2022 12:37:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 12:23:48 PM UTC-8, Bill Honaker wrote:
    On Tue, 11 Jan 2022 11:42:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote: >> >> 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

    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.
    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

    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.
    Keith,

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Keith Dick@21:1/5 to rupso...@gmail.com on Wed Jan 12 02:26:13 2022
    On Tuesday, January 11, 2022 at 11:40:54 PM UTC-8, rupso...@gmail.com wrote:
    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.
    On Wednesday, January 12, 2022 at 4:34:29 AM UTC+5:30, Bill Honaker wrote:
    On Tue, 11 Jan 2022 12:37:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 12:23:48 PM UTC-8, Bill Honaker wrote:
    On Tue, 11 Jan 2022 11:42:34 -0800 (PST), Keith Dick <rkd...@gmail.com> wrote:

    On Tuesday, January 11, 2022 at 10:15:31 AM UTC-8, Bill Honaker wrote: >> >> 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

    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.
    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

    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.
    Keith,

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

    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)