2

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?

mike
  • 1,279
  • 1
  • 13
  • 32
selpatS20
  • 55
  • 2
  • 8

3 Answers3

2

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                                             
dmc
  • 2,486
  • 20
  • 23
  • All this returns is 00123 instead of "#" (Using your number as an example). – selpatS20 Nov 14 '14 at 16:06
  • I misunderstood the question. Edited the example program to give the EBCDIC character based on a numeric value instead of just converting the number to character. – dmc Nov 14 '14 at 16:15
1

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,

user2338816
  • 2,113
  • 9
  • 11
  • A nice alternative. I'm still working on a V5R4 system, so my answer is definitely written from that perspective. – dmc Nov 15 '14 at 17:36
0

You need to use

CHGVAR   VAR(&CHAR)  VALUE(&NUM)
Abercrombieande
  • 581
  • 4
  • 12
  • All this returns is the number left padded with zeros. – selpatS20 Nov 14 '14 at 16:09
  • You really need to be careful of what you are doing. Are you calling an RPG program? You can pass everything as *CHAR even if the RPG program takes numerics. The reason for being careful is that literals are passed with default formats – Abercrombieande Nov 14 '14 at 16:32