IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

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: 8569
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: 8569
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: 8569
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Date conversion using SORT SYNCSORT 4
No new posts include cond ascii constant DFSORT/ICETOOL 4
This topic is locked: you cannot edit posts or make replies. Write Character to Hexadecimal file w... COBOL Programming 8
No new posts Date Conversion Y2T to Y2W SYNCSORT 8
No new posts XYplorer - EBCDIC to ASCII translatio... All Other Mainframe Topics 0
Search our Forums:

Back to Top