# How can I convert alpha input to hex for computation?

 Document ID : KB000033698 Last Modified Date : 14/02/2018 Show Technical Document Details
 QUESTION:How can I convert input alpha characters representing hex characters to actual hex fields for arithmetic computations? In this case the input is 2 records with field values 000000C0 and 00000020. The goal is to add hex C0 (decimal 192) and hex 20 (decimal 32) resulting in decimal 224.    Answer:The following codes use a translation table to convert each input character to the corresponding numeric value, indexing across input and output fields. This first is valid to FFFFFFFE. The second works for FFFFFFFF as well.//SYSIN DD *FILE FILEA  FA     40  8 A  FA-BYTE   FA    1  A  OCCURS 8 INDEX(INXA)FILE HEXTRAN TABLE INSTREAM  ARG  1 1 A  DESC 3 2 NA 10B 11C 12D 13E 14F 150 001 012 023 034 045 056 067 078 089 09ENDTABLE  NIBBLE  W  2 N  TOP     W  1 B  BOTTOM  W  1 B  BYTESUM W  2 B  BYTE    BYTESUM +1 1 B  FX      W  4 B  FX-BYTE   FX    1  B  OCCURS 4 INDEX(INXX)  SUMB    W 18 N   VALUE 0*JOB INPUT FILEA      INXA = 0      INXX = 0*      DO WHILE INXA < 8         INXA = INXA + 1DISPLAY 'ALPHA TOP ' FA-BYTE(INXA)      SEARCH HEXTRAN WITH FA-BYTE(INXA) GIVING NIBBLE             TOP = NIBBLE * 16         INXA = INXA + 1DISPLAY 'ALPHA BOT ' FA-BYTE(INXA)      SEARCH HEXTRAN WITH FA-BYTE(INXA) GIVING NIBBLE             BOTTOM = NIBBLE             BYTESUM = TOP + BOTTOM               INXX = INXX + 1               FX-BYTE(INXX) = BYTE      END-DO*DISPLAY 'FIELD'DISPLAY HEX FXSUMB = SUMB + FXDISPLAY 'SUM'DISPLAY HEX SUMB/*  YIELDING THE FOLLOWING DISPLAYS:ALPHA TOP 0                 ALPHA BOT 0                 ALPHA TOP 0                 ALPHA BOT 0                 ALPHA TOP 0                 ALPHA BOT 0                 ALPHA TOP C                 ALPHA BOT 0                 FIELD                       CHAR                        ZONE 000C                   NUMR 0000                        1...                   SUM                         CHAR 000000000000000192     ZONE FFFFFFFFFFFFFFFFFF     NUMR 000000000000000192          1...5...10...15...     ALPHA TOP 0                   ALPHA BOT 0                   ALPHA TOP 0                   ALPHA BOT 0                   ALPHA TOP 0                   ALPHA BOT 0                   ALPHA TOP 2                   ALPHA BOT 0                   FIELD                         CHAR                          ZONE 0002                     NUMR 0000                          1...                     SUM                           CHAR 000000000000000224       ZONE FFFFFFFFFFFFFFFFFF       NUMR 000000000000000224            1...5 The foloowing can be used as an alternative and works for FFFFFFFF as well://SYSIN DD *FILE FILEA  XIN            1  8  A  XIN-BYTE   XIN    1  A  OCCURS 8 INDEX(IDX)*FILE HEXTRAN TABLE INSTREAM  ARG  1  1 A  DESC 4 12 NA  000000000010B  000000000011C  000000000012D  000000000013E  000000000014F  0000000000150  0000000000001  0000000000012  0000000000023  0000000000034  0000000000045  0000000000056  0000000000067  0000000000078  0000000000089  000000000009ENDTABLE*N1      S 12 N   VALUE 0SUMNUM  S 12 N   VALUE 0*JOB INPUT FILEA IDX = 0 DO WHILE IDX < 8    IDX = IDX + 1    SEARCH HEXTRAN WITH XIN-BYTE(IDX), GIVING N1       CASE IDX          WHEN 7             N1 = N1 * 16          WHEN 6             N1 = N1 * 256          WHEN 5             N1 = N1 * 4096          WHEN 4             N1 = N1 * 65536          WHEN 3             N1 = N1 * 1048576          WHEN 2             N1 = N1 * 16777216          WHEN 1             N1 = N1 * 268435456       END-CASE    SUMNUM = SUMNUM + N1 END-DO/*