View previous topic :: View next topic
|
Author |
Message |
JPLMARTINS
New User
Joined: 28 Sep 2019 Posts: 2 Location: Portugal
|
|
|
|
Hi everyone,
we are in need of converting a String to his equivalent Decimal/ASCII value but until now we couldnĀ“t done it. In JAVA it's ok but in COBOL we didn't figure it out yet.
An example:
Input (String): 264189
Output (decimal/ascii): 50 54 52 49 56 57
Any help would be great! Thanks very much. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Use FUNCTION ORD to convert each character of the string to its ordinal value, subtract ORD('0') from the ordinal value, and add 49 to the result. |
|
Back to top |
|
|
JPLMARTINS
New User
Joined: 28 Sep 2019 Posts: 2 Location: Portugal
|
|
|
|
Hi,
tanks very much Robert Sample. I tested it and it's very close bur for example for value "2" wich i would expect getting 50 i'am getting 51. I could add 48, but it's correct that way?
Thank you very much.
INITIALIZE WS-INPUT
MOVE '2' TO WS-INPUT
COMPUTE WS-INPUT-NUM = FUNCTION ORD (WS-INPUT)
COMPUTE WS-OUTPUT-NUM = WS-INPUT-NUM - FUNCTION ORD ('0')
COMPUTE WS-OUTPUT-NUM = WS-OUTPUT-NUM + 49 |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
Yes that would do it. |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
FUNCTION ORD returns a value from 1 to 256, not 0 to 255. Hence adding 48 (instead of 49) will adjust the value correctly -- I hadn't considered that when making my first post. |
|
Back to top |
|
|
|