IBM Mainframe Forum Index
 
Register
 
IBM Mainframe Forum Index Mainframe: Search Log in to check your private messages Log in
 

String to ASCII conversion


 
IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: :: View next topic  
Author Message
JPLMARTINS

New User


Joined: 28 Sep 2019
Posts: 2
Location: Portugal

PostPosted: Sun Sep 29, 2019 12:00 am
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8568
Location: Dubuque, Iowa, USA

PostPosted: Sun Sep 29, 2019 1:49 am
Reply with quote

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
View user's profile Send private message
JPLMARTINS

New User


Joined: 28 Sep 2019
Posts: 2
Location: Portugal

PostPosted: Mon Sep 30, 2019 2:49 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8568
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2019 4:29 pm
Reply with quote

Yes that would do it.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8568
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2019 7:26 pm
Reply with quote

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
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic All times are GMT + 6 Hours
Forum Index -> COBOL Programming
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Forum Replies
No new posts XYplorer - EBCDIC to ASCII translatio... All Other Mainframe Topics 0
No new posts Need to split single line into two li... DFSORT/ICETOOL 7
No new posts Date format conversion DFSORT/ICETOOL 4
No new posts EBCDIC to ASCII conversion help All Other Mainframe Topics 1
No new posts how to convert Hex String t char hex PL/I & Assembler 6

Back to Top