General Page
You are in: RPG Cafe > Avoid X'7D'
Short URL: https://ibm.biz/rpgcafe_avoid_x7d
RPG Cafe: Don't use x'7D'
Everyone knows what X'7D' means ... unfortunately
cmd = 'ADDPFM LIB/FILE MBR(' + mbr + ') ' + 'TEXT(' + X'7D' + text + X'7D' + ')';
Unfortunately, too many RPG programmers know what X'7D' means. It is a common coding technique to code X'7D' when a single quotation mark is wanted in an RPG program. However, using X'7D' for a single quotation mark can be unclear for some programmers, and it could give incorrect results in some cases.
RPG has syntax for a single quotation mark character. Similar to 'A' being a single A character, you code '''' for a single quotation mark character. The inner quote has to be doubled because that's how RPG handles quotes within a character literal.
Which is more readable, X'7D' or ''''? Neither is readable.
Instead of using either X'7D' or '''' within your calculations, use a named constant. Call it QUOTE or SINGLE_QUOTE or APOSTROPHE. Define it in a /copy file and copy it in whenever you need it.
cmd = 'ADDPFM LIB/FILE MBR(' + mbr + ') ' + 'TEXT(' + QUOTE + text + QUOTE + ')';
The new question is "How to define the named constant?" In one way, it doesn't matter, since the meaning of the constant is clear from its name.
But it must be defined as '''':
- X'7D' is not a single quotation mark in ASCII or UTF-8. If you append X'7D' to an ASCII or UTF-8 string in RPG, it is treated as a hexadecimal literal in ASCII or UTF-8. You do not get the results you expect.
Try this program:
**free dcl-s fld_job char(3) ccsid(*jobrun); dcl-s fld_utf8 char(3) ccsid(*utf8); dcl-c BAD_QUOTE x'7d'; dcl-c GOOD_QUOTE ''''; fld_job = 'abc'; fld_utf8 = 'abc'; dsply (fld_job + BAD_QUOTE); dsply (fld_utf8 + BAD_QUOTE); dsply (fld_job + GOOD_QUOTE); dsply (fld_utf8 + GOOD_QUOTE); return;
It displays the following:
DSPLY abc' DSPLY abc} DSPLY abc' DSPLY abc'
Was this topic helpful?
Document Information
Modified date:
16 December 2019
UID
ibm11119369