Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

HEX to ASCII conversion

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
Dave Chin

New User


Joined: 14 Aug 2012
Posts: 3
Location: Malaysia

PostPosted: Thu Aug 23, 2012 1:55 pm    Post subject: HEX to ASCII conversion
Reply with quote

Hello Folks,

Tested a code for converting HEX values into ASCII format. Areas of usage would be places where in photos , finger prints, passwords etc need to be converted and stored into DB2 in ASCII(byte) format.

Since mainframes has no place for ASCII , you may have to receive data in Hex format and pass these values to below code and get the corresponding ASCII values. Every 2 chars of Hex gets converted to 1 char of ASCII(byte).

Again there are two types of HEX i.e. EBCDIC_Hex and ASCII_Hex.
So you may see two attachments which show the conversion tables, you may use either with the COBOL code below.

Code:
DCLGEN:
EXEC SQL DECLARE PHOTO_TAB TABLE
(
PHOTO                    VARCHAR(2900) NOT NULL
)

******************************
* COBOL DECLARATION FOR TABLE
******************************
01  TPHOTO.                                       
    10 PHOTO.                               
       49 PHOTO-LEN   PIC S9(4) USAGE COMP.
       49 PHOTO-TEXT PIC X(2900).                           

******************************
WORKING-STORAGE SECTION.
******************************
05 CTR3                 PIC 9(5) VALUE 0.
05 CTRH                 PIC 9(5) VALUE 0.

01  WS-PHOTO-HEX             PIC  X(5800).
01  WS-PHOTO-HEX-TWO     PIC  X(2).   
01  WS-PHOTO-ASCII          PIC  X(2900).

PROCEDURE DIVISION.

0000-MAIN-PROCESS.

MOVE 1                          TO CTRH           
MOVE 1                          TO CTR3           
MOVE LENGTH OF PHOTO-TEXT TO PHOTO-LEN
MOVE WS-VALUE(1:5800)   TO WS-PHOTO-HEX
MOVE FUNCTION UPPER-CASE(WS-VALUE(1:5800))           
                                   TO WS-PHOTO-HEX   
PERFORM 3000-MAP-ASCII  THRU 3000-MAP-ASCII-EXIT     
        VARYING CTRH  FROM 1 BY 1 UNTIL CTRH > 5800             


*--------------------------------------------------------* 
*          MAP INPUT HEX TO ASCII                        * 
*--------------------------------------------------------* 
                                                           
 3000-MAP-ASCII.                                           
                                                           
     MOVE WS-PHOTO-HEX(CTRH:2)       TO WS-PHOTO-HEX-TWO.   
      COPY HEX2ASCII.
*    COPY HEXTASCI
     ADD 1                           TO CTRH.               
     ADD 1                           TO CTR3.               
                                                           
 3000-MAP-ASCII-EXIT.                                       
     EXIT.                                                 
                                                           

MOVE WS-PHOTO-ASCII         TO CMNCS-PHOTO-TEXT.

*--------------------------------------------------------* 
*          INSERT RECORD INTO PHOTO_TABLE             * 
*--------------------------------------------------------* 
 4000-INSERT-REC.           
                               
EXEC SQL                         
    INSERT INTO PHOTO_TAB   
      (
        PHOTO
      )             
    VALUES                       
      (   
       :PHOTO
      )             
   END-EXEC.                     

STOP-RUN


Code'd
Back to top
View user's profile Send private message

Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 7913
Location: Bellevue, IA

PostPosted: Thu Aug 23, 2012 4:37 pm    Post subject:
Reply with quote

1. There is no such thing as "EBCDIC hex" or "ASCII hex". There is hexadecimal (hex), which is merely a way of grouping bits to reduce the size of the data being dealt with (X'5A' instead of B'01011010'). There is the EBCDIC collating sequence (also called code page), and ASCII collating sequence, and there are MANY variations of each. A particular hex byte such as X'5A' can represent different things in different collating sequences (such as X'5A' represents ! in EBCDIC or Z in ASCII).

2. Pictures are binary in nature -- their hex values do NOT change whether or not you are using EBCDIC or ASCII. I have taken GIF and JPG pictures on a PC (which uses ASCII), transferred them in binary to a mainframe that uses EBCDIC, and been able to view the picture via a web browser directly from the mainframe.

3. There is a link to manuals at the top of the page. Click on it, find the COBOL manuals, and read up on NATIONAL and CODE-SET (along with the CODEPAGE compiler option). These allow you to manipulate data from other collating sequences in COBOL.

However, you also need to go back and review your entire post. Much of what you said is complete and utter garbage based upon an erroneous understanding of EBCDIC, ASCII, and hex.
Back to top
View user's profile Send private message
Dave Chin

New User


Joined: 14 Aug 2012
Posts: 3
Location: Malaysia

PostPosted: Thu Aug 23, 2012 5:57 pm    Post subject: Reply to: HEX to ASCII conversion
Reply with quote

Well Mr. Robert, the below code isn't a code written on a fly . It works in cases when data has to be recieved from Java via MQ series , and stored in DB2 in bytes sequence or bit format. Yes I may have wrongly interpreted words but as such the above example is tested and live.

As experts one may always have a solution more feasible than the other which should always be encouraged
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7228

PostPosted: Thu Aug 23, 2012 7:00 pm    Post subject: Reply to: HEX to ASCII conversion
Reply with quote

"tested and live" is it?

Is there a "bounty" you're willing to put up per bug/stupidity? How much?
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Thu Aug 23, 2012 7:40 pm    Post subject: Reply to: HEX to ASCII conversion
Reply with quote

before posting code You should be sure to use the proper terminology
and clear withing Yourself the basics concepts

given Your snottiness I checked the attachment

hex2ascii the name is completely misleading ...

You are not translating anything to ascii ...
You are translating ascii to ebcdic

Your code takes a string of something encoded in base 16 and
translates each ASCII byte to the corresponding EBCDIC

as per ( Your code, not mine ) hex2ascii

Code:
               WHEN '30'
                   MOVE x'F0'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '31'
                   MOVE x'F1'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '32'
                   MOVE x'F2'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '33'
                   MOVE x'F3'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '34'
                   MOVE x'F4'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '35'
                   MOVE x'F5'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '36'
                   MOVE x'F6'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '37'
                   MOVE x'F7'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '38'
                   MOVE x'F8'          TO WS-PHOTO-ASCII(CTR3:1)
               WHEN '39'
                   MOVE x'F9'          TO WS-PHOTO-ASCII(CTR3:1)
 


the x'303132333436363738390 ==> c'0123456789' ( ASCII)
are plainly translated to the corresponding EBCDIC
c'0123456789' ==> x'f0f1f2f3f4f5f6f7f8f9'

so ... before posting horse manure meditate, please ... meditate

what You posted might work for You organization for some specific problem,

but in noo way does what the topic title suggests

the topic is going to be deleted very shortly,
we do not like having misleading information posed ...

another note ...
the hexascii symply restores a 2L string of base16 encoded data to a L string of binary data
Back to top
View user's profile Send private message
Dave Chin

New User


Joined: 14 Aug 2012
Posts: 3
Location: Malaysia

PostPosted: Thu Aug 23, 2012 8:32 pm    Post subject:
Reply with quote

Will take some snap shots of some example photo, its conversion to hex( input data ) and the snap shots of how it stores in Db2 and let's discuss as this one is sure to help out many. It justs that the work has been has potrayed with wrong terminology
Back to top
View user's profile Send private message
enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10202
Location: italy

PostPosted: Thu Aug 23, 2012 8:40 pm    Post subject: Reply to: HEX to ASCII conversion
Reply with quote

Quote:
It justs that the work has been has potrayed with wrong terminology


not Our problem... If You do not have the skill to describe things in the proper way
anything You might post will just make people waste everybody' s time
with useless questions.

( and especially the people with more experience capable of determining that what You post is just wrong )

as I said before it might work in Your organization for some obscure reasons,
but most certainly it will not be exportable.

Quote:
As experts one may always have a solution more feasible than the other which should always be encouraged


as an expert I strongly encourage people not to trust solution posted by somebody who is not capable off describing them using the proper terminology

the only thing that You have shown is how ...
given a base 16 encoded 2L string convert it to a binary L string
applying some arbitrary translation table ...

something that has been described much better in many other posts on this forum

do not post anything that will not pass a close scrutiny from the moderators
WE DO NOT WANT MISLEADING INFORMATION POSTED
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1278
Location: Belgium

PostPosted: Thu Aug 23, 2012 9:02 pm    Post subject:
Reply with quote

picture in ascii : http://cdn.walyou.com/wp-content/uploads//2011/06/F5PM9I5F4ULN87C.MEDIUM.jpg icon_wink.gif
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7228

PostPosted: Thu Aug 23, 2012 9:42 pm    Post subject: Re: HEX to ASCII conversion
Reply with quote

Hello folks,

Why would the file be represented by magic as a text string in HEX, with A-F in lower-case (a-f)?

Why use "counters" which are DISPLAY numeric?

Why give those counters such pathetic names?

Why do they have a VALUE when before anything is done to them, one is MOVEd to them?

Why "calculate" the length of the 2900-byte field, but not that of the 5800-byte field?

Why have two "counters" when their values are always in step?

Why use reference-modification at all?

Why use reference-modification when the length of the sending field is the length of the receiving field?

Why do two consecutive MOVEs to WS-PHOTO-HEX? It can only hold the value of one field at a time.

You are VARYING > 5800, which allows your code to go "outside" the field of 5800 bytes (the final iteration gives 5800 for a length of two).

I think that that is enough for such a short piece of code.

I don't think anyone will miss not having this code available, even as an outline, since it is such an odd way to go about it.
Back to top
View user's profile Send private message
mtaylor

Active User


Joined: 20 Feb 2009
Posts: 108
Location: Kansas City

PostPosted: Tue Oct 09, 2012 8:33 am    Post subject:
Reply with quote

FYI the 'standard' way to transmit/store binary data as text is to use base 64 encoding.
Back to top
View user's profile Send private message
Bill Woodger

DFSORT Moderator


Joined: 09 Mar 2011
Posts: 7228

PostPosted: Tue Oct 09, 2012 4:48 pm    Post subject: Reply to: HEX to ASCII conversion
Reply with quote

Since the topic is not quite discussing that, do you want me to split to another topic so that people can find it?
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 File Aid to File Manager conversion murali3955 IBM Tools 4 Thu Nov 24, 2016 3:41 pm
No new posts Conversion of a VB file to FB. isssal DFSORT/ICETOOL 1 Mon Oct 31, 2016 10:23 pm
No new posts EBCDIC to ASCII conversion using OCOPY mistah kurtz All Other Mainframe Topics 5 Tue Sep 06, 2016 2:26 pm
No new posts Comparex utility to Syncsort conversion mistah kurtz SYNCSORT 11 Fri Jul 22, 2016 1:53 pm
No new posts changing z/os unicode conversion service meykh2014 All Other Mainframe Topics 0 Tue Jul 12, 2016 10:49 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us