I am needing a really simple way to convert an integer to ASCII hex in RPG.
Announcement
Collapse
No announcement yet.
Integer To Hex
Collapse
X
-
This has been discussed on this list (and others) many times. This post gives good examples of converting both to and from hex.
http://www.code400.com/forum/forum/i...er-data-to-hex
To adapt this to handling an integer you can either remap the integer in question as a char field. e.g.
Code:dcl-ds convertData; integer int(10); // Or 5 or .... end-ds; integer = yourInteger; // Now use convertData as the source for the conversion and use the size in bytes of integer to define the input length.
Code:dcl-pr ToHex EXTPROC('cvthc'); szHexVal pointer Value; szCharVal pointer Value; nSrcLen int(10) Value; end-pr; ToHex( %Addr(Hex) : %Addr( yourInteger ) : %SIZE( yourInteger ) ); // NOTE %Size NOT %Len!!
-
May be I'm wrong, ... but why not simply using a data structure and overlaying the Integer Field with an Character Field?
Something like this. HEX includes the HexValue
Code:DCL-DS CvtHex; Byte Int(3); HexByte Char(1) Pos(1); SmallInt Int(5) Pos(1); HexSmallInt Char(2) Pos(1); Integer Int(10) Pos(1); HexInteger Char(4) Pos(1); BigInt Int(20) Pos(1); HexBigInt Char(8) Pos(1); End-DS;
If you need the integer value 256 to be converted into 0100, you can use the SQL Function HEX:
Code:Exec SQL Set :YourCharVal = Hex(:YourInteger);
Comment
Comment