Decimal To Character Conversion On AS/400

4.8k views Asked by At

In a CL, I am trying to convert a number (193) to its alpha representation (A). Coming up with nothing, need a little help. Is there a CHAR function?

3

There are 3 answers

2
dmc On BEST ANSWER

This example gives the EBCDIC character "A" in variable &TXT1:

PGM                                                
DCL        VAR(&NUM) TYPE(*DEC) LEN(3 0) VALUE(193)
DCL        VAR(&TXT2) TYPE(*CHAR) LEN(2)           
DCL        VAR(&TXT1) TYPE(*CHAR) LEN(1)           

CHGVAR     VAR(%BIN(&TXT2 1 2)) VALUE(&NUM)        
CHGVAR     VAR(&TXT1) VALUE(%SST(&TXT2 2 1))       
SNDUSRMSG  MSG(&TXT1)                              
ENDPGM                                             
2
Abercrombieande On

You need to use

CHGVAR   VAR(&CHAR)  VALUE(&NUM)
1
user2338816 On

Simplest in any recent OS release is to redefine, or overlay, the numeric with a character definition:

   dcl   &numVal      *uint     2     value( 193 )
   dcl   &charVal     *char     1     stg( *DEFINED ) defvar( &numVal   2 )

In a simple CL program, it might look like this:

pgm


   dcl   &numVal      *uint     2     value( 193 )
   dcl   &charVal     *char     1     stg( *DEFINED ) defvar( &numVal   2 )

   /* Show current character equivalence... */
   sndusrmsg   msg( &charVal ) msgtype( *INFO )

   /* Set a new numeric value... */
   chgvar      &numVal         ( 194 )

   /* Show new character equivalence... */
   sndusrmsg   msg( &charVal ) msgtype( *INFO )

   return

endpgm

The &charVal value will be displayed as "A" the first time and "B" the second. The *UINT variable must be defined as a 2-byte or larger variable since CL can't define integer variables of a single byte. The second byte of a 2-byte integer has the needed bit pattern. The binary integer value has a hexadecimal equivalent in memory that corresponds to character "A", "B" or whatever,