Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Forum Index
 
Register
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
String to ASCII conversion

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: String to ASCII conversion
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: 8505
Location: Dubuque, Iowa, USA

PostPosted: Sun Sep 29, 2019 1:49 am    Post subject: Reply to: String to ASCII conversion
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    Post subject: Reply to: String to ASCII conversion
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: 8505
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2019 4:29 pm    Post subject:
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: 8505
Location: Dubuque, Iowa, USA

PostPosted: Mon Sep 30, 2019 7:26 pm    Post subject:
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Bad arithmetic conversion when assign... Ajay Baghel CLIST & REXX 4 Tue Nov 26, 2019 11:29 am
No new posts Convert string to packed format ajaydwivedi4u COBOL Programming 6 Sat Sep 21, 2019 5:01 pm
No new posts Pack to Unpack conversion for sending... A_programmers COBOL Programming 13 Wed Sep 11, 2019 10:46 pm
No new posts Alphanumeric to Packed Decimal Conver... Vijay_Sirisha COBOL Programming 2 Mon May 27, 2019 2:52 am
No new posts data conversion error maxsubrat PL/I & Assembler 4 Thu May 09, 2019 10:57 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us