Sat, Apr
5 New Articles

Character-to-Hex Conversion

  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times
In reviewing the Work with Database File (WRKDBF) command for the previous issue of Midrange Developer, I was reminded of the number of times I've needed to convert data from its character value to its hexadecimal representation. For example, to convert 'A' to X'C1', I've used numerous techniques--from using an array lookup to moving a field to an integer, multiplying it by 256, and taking the remainder.

I retired all those tired old techniques nearly 10 years ago. Today, I let the operating system do the work. Buried inside the iSeries 400 is a machine interface (MI) that performs the work for high-level languages (HLLs). These so-called MI instructions may be called by RPG IV or C programs.

The first MI instruction set appeared on the System/38, where it was difficult to get at, but one smart developer figured out how to tap into it. That programmer was Ken Kelley, now president of Advanced Systems Concepts, Inc., the makers of ABSTRACT and SEQUEL. In the mid-1980s, Ken published his methods for patching the CL compiler to access MI. This opened up a new world of development that slowly made its way to the mainstream environment. Today, we don't need to know the secrets of patching compilers to get at the MI because IBM actually provides an MI compiler with OS/400.

To provide easier access to those very useful MI instructions, IBM created a set of wrapper functions written for the C compiler. This means that you can actually write MI instructions using C on the iSeries or AS/400. For example, in MI there is a cvthc (convert character to hex) instruction that does in one instruction what it used to take countless lines of hand-written RPG code to do.

Okay, you're an RPG programmer. So who cares about C anyway? The answer is, you do--because C is a powerful language on the AS/400. You can access the entire C runtime library and the MI instruction "library" from RPG IV. The OS/400 C compiler contains the traditional C language functions, such as memset(), sprintf(), time(), and so forth, which are part of the C runtime library.

So how do you get at the C runtime library, and consequently the MI instructions, without learning the C language? That's the easy part; it's called program binding. Program binding gives RPG IV the ability to call procedures written in any programming language. To use any MI instruction or C language runtime function in RPG IV, you need to prototype it and link it to the C runtime library, using a binding directory. In the C language, the entire runtime library of functions is actually a set of procedures that are callable from any HLL that supports binding, including good old RPG IV. This means that just about any function in the C language can be called from within RPG IV, including the MI instruction set.

So is RPG IV more powerful than C? Well, RPG IV can call virtually every runtime function of C, and C can call procedures written in RPG IV, but C cannot call any RPG IV operation codes or built-in functions.

Let me show you how to use the C runtime library functions in your own RPG IV code. Two MI instructions and two C functions immediately come to mind as being useful. The MI instructions provide a simple way to convert from and to hex. The C functions do two things: one allows you to move a character repeatedly to a target memory location; the other converts character values into numeric.

The two MI instructions that perform conversion from hexadecimal to character and back again are cvthc and cvtch. I've seen dozens of routines that convert the character string 'C1' into the letter 'A' and back again. Most involve a subroutine that uses math and shifting of bits. All these routines became obsolete with the introduction of these two MI instructions.

  • cvthc--Converts from a character value to the hexadecimal text of the character values (e.g., from 'A' to X'C1')

  • cvtch--Converts from hexadecimal text to the character form of the hex values. (e.g., from X'C1' to 'A')

The interesting thing about these instructions is that they support conversion of single or multiple characters to and from hexadecimal. So you need only call these routines once per field rather than once per character.

Figure 1 shows the prototype for both MI instructions. To use them, simply store the prototypes in a separate source member and use /COPY or the new /INCLUDE directive to include them in your program.

The 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.

0001 H  BNDDIR('QC2LE')
0002 D ToHex           PR                  EXTPROC('cvthc')
0003 D  szHexVal                   4096A   OPTIONS(*VARSIZE)
0004 D  szCharVal                  2048A   OPTIONS(*VARSIZE)
0005 D  nSrcLen                      10I 0 VALUE
0006 D FromHex         PR                  EXTPROC('cvtch')
0007 D  szCharVal                  2048A   OPTIONS(*VARSIZE)
0008 D  szHexVal                   4096A   OPTIONS(*VARSIZE)
0009 D  nSrcLen                      10I 0 VALUE

Figure 1: Prototypes for cvthc and cvtch MI Instructions

Note that on line 1 in Figure 1, the BNDDIR('QC2LE') keyword is specified. This is required when your program is taking advantage of any C runtime library functions. Do not include that line in the /COPY member, but rather 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 that binding directory, the RPG compiler binds to the C runtime functions. No additional programming required!

Other interesting C runtime functions can be found in the QCLE library (Version 4) or the QCPPLE library (Version 5 and later). The QACSRC source file contains examples of how to use all MI and appropriate C runtime functions. These examples, however, are written in the C language.

The C language's memset() function is an interesting utility. Memset() copies a single character repeatedly to a target memory location (e.g., a field). While this is similar to the MOVE *ALL'c' capability of RPG, memset() can be used to copy data to dynamically allocated memory locations. So if you don't know the size until runtime, memset() is a good choice.

0001 D memset          PR                  EXTPROC('memset')
0002 D  pData                          *   VALUE
0003 D  cCharToSet                    1A   VALUE
0004 D  nByteCount                   10I 0 VALUE

Figure 2 : Prototype for the Memset() Function

Figure 2 shows the prototype source code for the memset() function. Note that each parameter is passed by value. By default, C passes parameters by value, whereas traditional OS/400 passes parameters by reference. To ensure that your code is compatible with the C language, use the VALUE keyword on the prototype.

Also note the EXTPROC keyword (line 1). This keyword identifies or names the function being prototyped. You need it when you're prototyping procedures or functions written in languages that support mixed names, such as C. That is, MEMSET is not the same as memset to a C program. So in RPG, we prototype it and enclose its real name (in lowercase) in single quotes: 'memset'. In RPG calculation specifications, you can use memset, Memset, MemSet, or MEMSET; it all reverts back to the 'memset' function.

The first parameter of memset() is the address of the memory that will receive the repeated character. Normally, you would specify either a pointer variable that contains the address of allocated memory or %addr(f) where 'f' is a field in your program. The %addr() built-in function returns the address of the field being addressed. Addresses are stored in pointer variables.

0001 H  BNDDIR('QC2LE')
0003 D pValue          S               *
0004 D Value           S          32766A   based(pValue)
0005 D nSize           S             10I 0
0006 D Name            S             30A
0007 C                   Eval      nSize = %size(Name) * 50
0008 C                   Alloc     nSize         pValue

0009 C                   CallP     memset(pValue : X'40': nSize)

Figure 3: Use Memset to Initialize Dynamic Storage

In Figure 3, the field pValue is a pointer whose value is set on line 7. The ALLOC operation code allocates the number of bytes of memory specified in Factor 2. The field nSize is used on line 7 to store the number of bytes to be allowed. The built-in function %size(Name) returns the value 30, which is then multiplied by 50. So 1500 bytes of memory are allocated on line 8. The address of that memory is returned and stored in the pValue field.

Then, on line 9, hex 40's are moved into the dynamically allocated storage. I could have used a simple ' ' (a quoted blank space) and gotten the same result, but I wanted to show you how to use hex values, since using hex values with memset() is much more common.

So, once line 9 is performed, 1500 bytes of new memory are allocated and set to blanks. For more information on the ALLOC or the REALLOC operation codes, see The Modern RPG IV Language. Next time, I'll show you how to use this technique to
dynamically allocate array elements at runtime. So remember to tell your


Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.

MC Press books written by Robert Cozzi available now on the MC Press Bookstore.

Get this jam-packed resource of quick, easy-to-implement RPG tips!
List Price $65.00

Now On Sale

The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?
List Price $99.95

Now On Sale



Support MC Press Online

$0.00 Raised:

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA 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.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra 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: