Incrementing a 2-Character Alphanumeric Code in RPG Free
Incrementing a 2-Character Alphanumeric Code in RPG Free
Today I’m sharing a small but useful RPG Free program to increment a 2-character alphanumeric code using the sequence A..Z, 0..9. The program also handles wrap-around after "99", restarting at "AA".
Source Code
You can copy and paste the code directly from the field below:
How It Works
- The program takes a 2-character input code.
- It validates that both characters are within the allowed sequence A..Z,0..9.
- It increments the second character and carries over to the first if necessary.
- If the code exceeds "99", it wraps back to "AA".
- If the input contains invalid characters, an error code INVALIDVAL is set and the output is cleared.
Ideas for Improvement
This simple module can be enhanced in several ways:
- Handle codes longer than 2 characters.
- Allow custom sequences, e.g., only letters or only numbers.
- Create a reverse function to decrement a code.
- Make the code case-insensitive.
Try it on your IBM i, experiment with improvements, and share your optimizations. Improving these small programs is a great way to become more skilled in RPG Free and DB2!

here's my version. drawback: you cannot have a longer inputValue string than 2 chars
ReplyDeletepersonally, I would have prefer to build a function that returns the outputValue as a result instead of an additional parm. but, that's fine. Enjoy!
**free
Ctl-Opt DftActGrp(*No) ActGrp(*New) Option(*SrcStmt:*NoDebugIO);
Dcl-PI RAAINCR;
inputValue Char(2) Const;
outputValue Char(2);
errorCode Char(10);
End-PI;
Dcl-C Base36 36;
Dcl-C ValidChars 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
Dcl-DS inputDS;
chr1 Char(1);
chr2 Char(1);
End-DS;
Dcl-S result Int(5);
Dcl-S pos1 int(3);
Dcl-S pos2 int(3);
// Split inputValue chars
inputDS = inputValue;
// Validate inputValue
pos1 = %Scan(chr1 :ValidChars);
pos2 = %Scan(chr2 :ValidChars);
If pos1 = 0 Or pos2 = 0;
errorCode = 'INVALIDVAL';
outputValue = *Blanks;
Return;
EndIf;
// Increment inputValue. Beyond '99' restart from 'AA'
result = %Rem(((pos1 - 1) * Base36 + (pos2 - 1) + 1) :Base36 * Base36);
chr1 = %SubSt(ValidChars :%Div(result :Base36) + 1);
chr2 = %SubSt(ValidChars :%Rem(result :Base36) + 1);
// Build the resulting code
outputValue = inputDS;
Return;