blog
blog copied to clipboard
Handling static EBCDIC hex in NetRPG
This is a fun post to write about, right? Everyone in the IBM i world loves to deal with EBCDIC problems. Of course, when porting RPGLE to another platform you have to handle edge cases of when hexadecimal can be used.
Firstly, you can read about hexadecimal literals here in the RPGLE manual.
Let's use this RPG example:
**FREE
Dcl-S MyStr Char(12);
MyStr = x'C88593939640A6969993845B';
Dsply MyStr;
That static hexadecimal string is Hello world$
in EBCDIC. Now, we must not forget that there are multiple versions of EBCDIC. If we were running this under a job of CCSID (or codepage) 37, then the value is Hello world$
. If we were running this under CCSID 285, it would be ``Hello world£`.
Luckily, for NetRPG, .NET Core has some support for these encodings (and others too!). The parser is keeping an eye out for these hexadecimal literals.
case "x":
token = new RPGToken(RPGLex.Type.STRING_LITERAL, EBCDIC.ConvertHex(EBCDIC.GetEncoding(int.Parse(Configurations["CCSID"])), tokens[i + 1].Value));
Using System.Encoding
we can convert a string of bytes from EBCDIC to a regular C# string. and we created a ConvertHex
function to make this easier long term. We also have a function to convert an IBM i CCSID to the equivalent .NET Core encoding name. You can see those here.
Meaning, when the hexadecimal literal is found in the RPG code, the tokens are replaced with the converted string. Because .NET Core supports multiple different CCSIDs, it means we can support them too!