Using %BIN to Change Case of Letters in CL Pgms

CL
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Using %BIN to Change the Case of Letters in CL Programs

The EBCDIC representations of lowercase letters differ from those of uppercase letters only in bit 1 (the second bit from the left). If you treat the characters as unsigned binary integers, then the lowercase letters are 64 less than their uppercase counterparts. In other words, you can add 64 to a lowercase letter to capitalize it, and you can subtract 64 from an uppercase letter to make it lowercase.

You can use this method along with the %BIN function to capitalize characters in a CL program. %BIN requires a two- or four-byte argument, so you'll need to move the character to the low-order byte of a two-byte character field which has been initialized to binary zero. Add 64 to the field and then move it back. Refer to 1 for an example.

You can use this method along with the %BIN function to capitalize characters in a CL program. %BIN requires a two- or four-byte argument, so you'll need to move the character to the low-order byte of a two-byte character field which has been initialized to binary zero. Add 64 to the field and then move it back. Refer to Figure 1 for an example.

You may also use this method to capitalize the diacritic characters (e.g., those with accent marks or tildes); but the difference between lowercase and uppercase is 32, not 64.


Using %BIN to Change Case of Letters in CL Pgms

Figure 1 Changing Case in CL

 DCL VAR(&BINVAL) TYPE(*CHAR) LEN(2) VALUE(X'0000') DCL VAR(&MESSAGE) TYPE(*CHAR) LEN(80) DCL VAR(&N) TYPE(*DEC) LEN(3) DCL VAR(&ONECHAR) TYPE(*CHAR) LEN(1) ... /* If the &Nth character of &MESSAGE is a lowercase letter, + capitalize it */ CHGVAR VAR(&ONECHAR) VALUE(%SST(&MESSAGE &N 1)) IF COND((&ONECHAR *GE 'a' *AND &ONECHAR *LE 'i') *OR + (&ONECHAR *GE 'j' *AND &ONECHAR *LE 'r') *OR + (&ONECHAR *GE 's' *AND &ONECHAR *LE 'z')) THEN(DO) CHGVAR VAR(%SST(&BINVAL 2 1)) VALUE(&ONECHAR) CHGVAR VAR(%BIN(&BINVAL)) VALUE(%BIN(&BINVAL) + 64) CHGVAR VAR(%SST(&MESSAGE &N 1)) VALUE(%SST(&BINVAL 2 1)) ENDDO 
BLOG COMMENTS POWERED BY DISQUS