ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Integer To Hex

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Integer To Hex

    I am needing a really simple way to convert an integer to ASCII hex in RPG.

  • #2
    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.
    Alternatively you could modify the prototype shown in the example to use pointers by value which makes it more flexible if a little less RPG like. e.g. (Not tested)

    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!!

    Comment


    • #3
      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 SmallInt is set to 256, the HEX value in HexSmallInt is x'0100'

      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);
      Birgitta

      Comment

      Working...
      X