About Translating Strings
Heres a simple way to externalize the XLATE op code.
RPGs XLATE op code is flexible, but it depends on program-defined tables to do its work. The subprocedure XLATE presented in this article uses external table objects (*TBL) to do the same thing, thus simplifying the programmers job and reducing the chances for error. As a bonus, you get to learn how to create your own translation tables.
Translating strings has always been one of those chores you wish you didnt have to do. To be sure, the Translate (XLATE) op code helps somewhat by allowing you to provide a program-described translation table. Still, in this age of object-oriented programming, using program-described pseudo-objects is cumbersome.
Thats why I decided to improve on the op code and create a subprocedure of the same name (XLATE) that can translate any given string using an external table object (*TBL). For illustration purposes, lets imagine you want to cypher some data using the popular ROT13 cryptographical scheme.
ROT13 is simple, so its easy to break, but it will do for illustration. Its premise is that all letters of the alphabet are rotated 13 positions. Since there are 26 letters in the English alphabet, it turns out that applying ROT13 twice restores the original string as it waswell, originally. To give you an idea, A becomes N, B becomes O, all the way to M, which becomes Z. Then N becomes A, O becomes B, all the way to Z, which becomes M.
Creating the Table
OS/400 gives us the tool to create an external table: the Create Table (CRTTBL) command. Although you can create a table without using source code, by specifying SRCFILE(*PROMPT), youre probably better off writing the source code and then using
that to create the table. The reason is simple: You may want to document a table, clone it to make one almost like it, whatever.
Table source code (source type TBL) must have exactly eight records of 64 bytes each. You can use any standard 92-byte source file, however. In the first record, you must code the hexadecimal values of each of the first 32 characters in the EBCDIC collating sequence (X00 to X2F). The rest of the records must contain the hex values of the remaining characters.
Then, you need to make changes to whatever values you want translated. In my ROT13 example, I change X81 (lowercase a) to X95 (lowercase n) and XC1 (capital
A) to XB5 (capital N), and then I make similar changes for the other 25 letters, in both lowercase and uppercase. The resulting source code is listed in Figure 1. You compile this code with the following command:
CRTTBL TBL(xxx/ROT13) +
SRCFILE(xxx/QTBLSRC)
The Subprocedure XLATE
Figure 2 shows the prototype Ill use for subprocedure XLATE. XLATE is designed to process strings of any length (up to the RPG limit of 32,767 bytes). It needs two parameters: the input string (i.e., the string you want to translate) and the name of the table object (in our case, the name is ROT13). XLATE then returns the translated string as a string of varying length up to 32,767 bytes.
Figure 3 shows the source code for RPG IV module XLATEMOD. Its workings are extraordinarily simple. The actual translation is performed by system API QDCXLATE, which uses fixed-length strings and requires, as its first parameter, the length of the input string. Since I designed XLATE to use varying-length strings, the calculation of this length can be performed by built-in function %LEN. After QDCXLATE returns control to my RPG function, indicator 01 is on if QDCXLATE ended in error (as would be the case if the table you supplied did not exist); in that case, QDCXLATE returns a hexadecimal FF; otherwise, it returns the actual translated string.
Youll notice the VARYING keywords in the D-specs; those define character strings of varying length. If youre using a version prior to V4R2, you wont be able to use VARYING. In that case, you can remove the keywords and make sure to pass XLATE strings of fixed size. Also, in that case, consider not translating the blank space to anything different so that the inevitable trailing blanks (of the fixed-size strings) arent converted to anything weird.
Finally, Figure 4 gives you an idea how to use XLATE. This test program receives a character string of up to 32 bytes, translates it, and displays the translated value. To do so, it invokes my function XLATE, supplying a table name of ROT13. To run the test program, execute the following command:
CALL XLATETEST string
In this command, string is the character string (up to 32 bytes) you want translated.
More Tables
Now that you know how to code table source code, you can create your own tables to suit your translation requirements. But dont reinvent the wheel! OS/400 contains a few predefined tables you can use right away. For example, QSYSTRNTBL in QSYS translates all lowercase letters to uppercase, QASCII translates EBCDIC to ASCII, and QEBCDIC does the opposite. If you need translation within a CL program, you can call QDCXLATE directly from it.
Finally, a warning. First, do not rely on ROT13 to keep your information confidential. As I mentioned earlier, its a weak encryption algorithm that any fourth grader can break.
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F
404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F
606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F
809596979899A2A3A4A58A8B8C8D8E8F90A6A7A8A981828384859A9B9C9D9E9F
A0A18687888991929394AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF
C0D5D6D7D8D9E2E3E4E5CACBCCCDCECFD0E6E7E8E9C1C2C3C4C5DADBDCDDDEDF
E0E1C6C7C8C9D1D2D3D4EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF
Figure 1: Source member ROT13
D xlate PR 32767A VARYING
D input 32767A VARYING VALUE
D tbl 10A VALUE
Figure 2: Prototype for XLATE subprocedure (XLATEPRO)
*=================================================================
* To compile:
*
* CRTRPGMOD MODULE(XXX/XLATEMOD) SRCFILE(XXX/QRPGLESRC) +
* TEXT(String translation module)
* CRTSRVPGM SRVPGM(XXX/XLATE) MODULE(XXX/XLATEMOD) +
* TEXT(String translation service program) +
* EXPORT(*ALL) ACTGRP(*CALLER)
*
*=================================================================
H NOMAIN
* Note: In the /COPY statement that follows,
* change the library and file name to suit your needs.
/COPY MAGWORK/SOURCE,XLATEPRO
P xlate B EXPORT
D xlate PI 32767A VARYING
D input 32767A VARYING VALUE
D tbl 10A VALUE
D output S 32767A VARYING
D work S 32767A
D worklen S 5P 0
C EVAL worklen = %LEN(input)
C EVAL work = %SUBST(input:1:worklen)
C CALL QDCXLATE 01
C PARM worklen
C PARM work
C PARM tbl
C IF *IN01
C EVAL output = XFF
C ELSE
C EVAL output = %SUBST(work:1:worklen)
C ENDIF
C RETURN output
P xlate E
Figure 3: Source for module XLATEMOD
*=================================================================
* To compile:
*
* CRTRPGMOD MODULE(XXX/XLATETEST) SRCFILE(XXX/QRPGLESRC) +
* TEXT(Test for XLATE)
* CRTPGM PGM(XXX/XLATETEST) MODULE(XXX/XLATETEST) +
* TEXT(Test for XLATE) +
* BNDSRVPGM(XXX/XLATE) ACTGRP(*CALLER)
*
*=================================================================
D input S 32A
D output S 32A
* Note: In the /COPY statement that follows,
* change the library and file name to suit your needs.
/COPY MAGWORK/SOURCE,XLATEPRO
C *ENTRY PLIST
C PARM input
C EVAL output = XLATE(input:ROT13)
C output DSPLY
C EVAL *INLR = *ON
LATEST COMMENTS
MC Press Online