I need to display the hex value of a file field value in display file in a rpgle program

160 Views Asked by At

I need to read a file using RPGLE and then display the Hexadecimal value of some of the fields of the file in a display file.

Can someone tell me how to get the hex value of a field in a rpgle program and display it in a display file.

Any suggestions will be really appreciated

1

There are 1 best solutions below

0
On

RPG IV doesn't have any built in HEX() function...

However, SQL does.

dcl-s myCharString varchar(100);
dcl-s myHexString varchar(200);

exec sql
  values hex(:myCharString) into :myHexString;

Optionally, you can use the MI Convert Hex to Character (CVTHC)* instruction using a prototype like so...

dcl-pr CharToHex ExtProc('cvthc');
   hexResult    Char(65534)  Options(*VarSize);
   charInput    Char(32767)  Options(*VarSize);
   charNibbles  Int(10)  Value;
End-Pr;

dcl-s myCharString varchar(100);
dcl-s myHexString varchar(200);

CharToHex ( myHexString : myCharString : %len(myCharString) * 2 );

*Note the MI name(s) are counter-intuitive, the reverse operation is
Convert Character to Hex (CVTCH).

Here's a good article by Jon Paris, Practicing Safe Hex in RPG, about the MI functions.