RPGIV @ Work

A unique site for RPG and System i Lovers

Welcome!

Hi, this site will provide all what you need in System i and RPG developments.

My Name is Chamara Withanachchi, System i Expert and RPG Developer. And in the field for last 11 years.

I hope you will find lot of valuable information from this site

A Quick-and-Easy Way to Convert Case in RPG Programs Print E-mail
User Rating: / 1
PoorBest 
Written by Chamara Withanachchi   

Several good ways exist to convert letters from uppercase to lowercase and vice versa. Some take more effort than others. Here's a very easy RPG method that you may not have seen.

Suppose a four-character program parameter is supposed to have one of three values: edit, post or both. Suppose further that you don't want case to matter. The user can enter uppercase, lowercase, or mixed-case values. How do you go about testing the parameter in your program?

You could do it the hard way, that is, test for all possible combinations, like this:

 /free
      if inOption = 'edit' or inOption = 'ediT'
         or inOption = 'edIt' or inOption = 'edIT'
         or inOption = 'eDit' or inOption = 'eDiT'
         or inOption = 'eDIt' or inOption = 'eDIT'
         or inOption = 'Edit' or inOption = 'EdiT'
         or inOption = 'EdIt' or inOption = 'EdIT'
         or inOption = 'EDit' or inOption = 'EDiT'
         or inOption = 'EDIt' or inOption = 'EDIT';  

But that's excessive, don't you think? Better to convert the value to all uppercase or lowercase, whichever you prefer, and test the converted value against one uppercase or lowercase constant. But how do you convert case?

The "proper" way would be to use the case-conversion APIs, QCLGNVCS and QlgConvertCase. They work well.

Or you could engage in a little bit-twiddling. The following example shows how to convert the afore-mentioned four-byte value to lowercase and uppercase:

 *** *entry plist ***
  D SomePgm         pr                  extpgm('SOMEPGM')
  D  inOption                      4a   const


  D SomePgm         pi
  D  inOption                      4a   const
  D Option          s              4a


    /free
       // convert to lowercase
       Option = %bitand(inOption: x'BFBFBFBF');
       // convert to uppercase
       Option = %bitor (inOption: x'40404040');  

Notice the hexadecimal constants in the second parameters of the bit functions. You'll need a hex BF or 40 for each character to be converted.

Now you can test the Option variable against your preference of EDIT or edit.

I don't know how this technique works with natural languages other than English. But I do know that it runs very fast, much faster than the APIs do.

<Previous   Next>