About three years ago, I wrote an article exposing the MI instructions that allow you to convert text data containing hexadecimal characters, such as F1F2F0F0F0, into their character format, such as 12000.
The two MI instructions that perform conversion from hexadecimal to character and back again are cvthc and cvtch. I've seen dozens of hand-written routines that convert the hexadecimal strings to letters and letters to hexadecimal strings. Sounds like reinventing wheel to me.
All these routines became obsolete with the introduction of these two MI instructions back in the 1980s. It wasn't until IBM introduced System/C back in the mid 1990s that it became practical to use them outside of MI, but now with RPG IV's ability to call C and MI runtime functions, there is no excuse to ask "How do I...?" in this case.
The two MI instructions are as follows:
- cvthc converts from character value to hexadecimal text (that is, from 'A' to X'C1').
- cvtch converts from hexadecimal text to character value (that is, from X'C1' to 'A').
The interesting thing about these instructions is that they support conversion of a single character or multiple characters at one time. So you need only call these routines once per value rather than once per character.
The prototype for both MI instructions is as follows:
.....D*ame+++++++++++EUDS.......Length+TDc.Functions+++++++++++++++
0002 D ToHex PR EXTPROC('cvthc')
0003 D szHexVal 65534A OPTIONS(*VARSIZE)
0004 D szCharVal 32767A Const OPTIONS(*VARSIZE)
0005 D nSrcLen 10I 0 VALUE
0006 D FromHex PR EXTPROC('cvtch')
0007 D szCharVal 32767A OPTIONS(*VARSIZE)
0008 D szHexVal 65534A Const OPTIONS(*VARSIZE)
0009 D nSrcLen 10I 0 VALUE
Note that I've named the prototypes differently than the MI instruction names. This is because I feel the MI names are too confusing. The TOHEX prototype converts normal character format into the two-character hexadecimal format. The FROMHEX prototype converts the two-character hexadecimal format to normal character format.
Hexadecimal representation of the value appears as pairs of characters. Consequently, the hexadecimal parameter must be at least twice the length of the character field. Also, the input value is specified on the second parameter, and the result is returned to the first parameter. Last, the length of the text to be converted (i.e., the length of the data in parameter 2) is specified on parameter 3.
To use these prototypes, simply store them in a separate source member and use /COPY or /INCLUDE to include them in your source member.
Note that on the first line in the example, the BNDDIR('QC2LE') keyword is specified. This is required when your program is taking advantage of any C or MI runtime functions. Do not include this line in the /COPY member; instead, put in the source member being compiled.
QC2LE is an IBM-provided binding directory that includes the names of the service programs the C runtime library uses. By using this binding directory, the RPG IV compiler binds to the C and MI runtime functions. No additional programming required!
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.



Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:
LATEST COMMENTS
MC Press Online