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 N

A 10

B 11

C 12

D 13

E 14

F 15

0 00

1 01

2 02

3 03

4 04

5 05

6 06

7 07

8 08

9 09

ENDTABLE

  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 + 1

DISPLAY 'ALPHA TOP ' FA-BYTE(INXA)

      SEARCH HEXTRAN WITH FA-BYTE(INXA) GIVING NIBBLE

             TOP = NIBBLE * 16

         INXA = INXA + 1

DISPLAY '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 FX

SUMB = SUMB + FX

DISPLAY '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 N

A  000000000010

B  000000000011

C  000000000012

D  000000000013

E  000000000014

F  000000000015

0  000000000000

1  000000000001

2  000000000002

3  000000000003

4  000000000004

5  000000000005

6  000000000006

7  000000000007

8  000000000008

9  000000000009

ENDTABLE

*

N1      S 12 N   VALUE 0

SUMNUM  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

/*