You are currently on IBM Systems Media’s archival website. Click here to view our new website.


A New Year's Potpourri of RPG Tips

Rather than focus on a single topic this month, we're going to begin the New Year with a selection of questions that we are frequently asked by our readers and students.

Converting from Numeric to Character

As more and more people make the transition to using the /Free form of RPG, a question that often arises is how, in the absence of the MOVE op-code, to convert from numeric to character. Not surprisingly, many people begin the quest by looking at the %Char built-in function (BIF). After all, its title "Convert to character data" does suggest that this is the function that we need. Indeed, converting from numeric to character is exactly what %CHAR does, but in many cases, it goes further than the programmer actually wants.

Take a look at the example program below. At (A) we use a conventional MOVE to convert the value in Num6 to character. At (B) we've attempted to do the same thing by using %CHAR. If you run the program however, you' ll find that the results of the two operations are different. The reason, quite simply, is that %CHAR not only converts the numeric field to character, but also trims off any leading zeros. In the case of our example, the value contained within the field Num6 only has four significant digits. Whereas MOVE preserves those initial zeros, %CHAR wants to try and be "helpful" by removing them for us. In this case this help isn't needed.

So what is the correct BIF to use? The answer is %EDITC, which allows you to apply an edit code to the operation and works exactly the same way as it would if you used that edit code on an O-spec. In the case in point, the appropriate edit code to use is 'X', which preserves the leading zeros as MOVE does. We demonstrate this at (C).

D Num6 s 6 0 Inz(1234)

D Char6 s 6Inz

(A) C Move Num6 Char6
C Char6 Dsply

C EvalChar6 = *Blanks

(B) C EvalChar6 = %Char(Num6)
C Char6 Dsply

C Eval Char6 = *Blanks

(C) C EvalChar6 = %EditC(Num6: 'X')
C Char6 Dsply

Although not illustrated in the example, it's wise to take into account the behavior difference between MOVE and EVAL. If the two fields are of unequal length, EVAL blank fills additional positions in the receiving field whereas MOVE retains the original content. If this is the behavior you require, then you'll also need to apply the BIF %SUBST to the result field. For example, if the MOVE operation you wish to replace is currently moving a six-digit numeric into an eight-character field, you would need to code the EVAL like so:

Eval%SUBST(Char8: 3) = %EditC(Num6: 'X')

Eval %SUBST(Char8: 1) = %EditC(Num6: 'X')

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

New and Improved XML-INTO

Namespace support makes the opcode a viable option

Authenticating on the Web

The finer points of OpenRPGUI, Part 1

The Microphone is Open

Add your voice: Should IBM i include open-source RPG tools?

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters