Convert Hex to Int

Posted by jmls on 25-Feb-2010 09:56

I know that from 10.0, you can say

def var i as int no-undo.

i =  0x4b7

but what if you have "0x4b7" as a string value ? How do you convert the string to int ?

Using 10.2B, if that helps ...

All Replies

Posted by Admin on 25-Feb-2010 10:27

Just client (so is GUI for .NET an option) or also AppServer?

Posted by ChUIMonster on 25-Feb-2010 10:40

In the i4gl you would:

  display integer( "0x4b7" ).

Unfortunately in the ABL that doesn't seem to work

Posted by jmls on 25-Feb-2010 10:42

Appserver & Webspeed as well

On 25 February 2010 16:27, Mike Fechner

Posted by mopfer on 25-Feb-2010 10:47

This is what we use.  It's adapted from some code that Kirt Eldredge put in the K-Base in 1999.


/* ************************  Function Implementations ***************** */

&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION f_HexToDec Include
FUNCTION f_HexToDec RETURNS INTEGER
  ( INPUT ipcHexValue AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: 
    Notes: 
------------------------------------------------------------------------------*/
DEFINE VARIABLE iIntegerValue AS INTEGER    NO-UNDO.
DEFINE VARIABLE iCounter      AS INTEGER    NO-UNDO.
 

/* hexadecimal should always be capitals, but... */
ipcHexValue = CAPS(ipcHexValue).

/* review each char and evaluate if it is an integer or letter.
  If it is a letter determine its value and add 10.
  Then multiply the char value times
  16 to the char position minus 1. EXAMPLE: ipcHexValue = 1180C
     Char 1 - 1 * 16 to the 4th (5 - 1) power = 65536
     Char 2 - 1 * 16 to the 3rd (5 - 2) power = 4096
     Char 3 - 8 * 16 to the 2nd (5 - 3) power = 2048
     Char 4 - 0 * 16 to the 1st (5 - 4) power = 0
     Char 5 - C or 2 + 10 * 10 to 0 power = 12
            integer value is 71692 */

DO iCounter = 1 TO LENGTH(ipcHexValue):

  IF CAN-DO("0,1,2,3,4,5,6,7,8,9", (SUBSTRING(ipcHexValue, iCounter, 1))) THEN
    iIntegerValue = iIntegerValue + INT(SUBSTRING(ipcHexValue, iCounter, 1)) *
                      EXP(16, (LENGTH(ipcHexValue) - iCounter)).
  ELSE
    iIntegerValue = iIntegerValue + (KEYCODE(SUBSTRING(ipcHexValue, iCounter, 1)) -
                      KEYCODE("A") + 10) * EXP(16, (LENGTH(ipcHexValue) - iCounter)).

END. /* do: iCounter = 1 to length(ipcHexValue) */


RETURN iIntegerValue.   /* Function return value. */

END FUNCTION.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME


&ANALYZE-SUSPEND _UIB-CODE-BLOCK _FUNCTION f_ConvertToHex Include
FUNCTION f_ConvertToHex RETURNS CHARACTER
  ( INPUT ipcValue AS CHARACTER ) :
/*------------------------------------------------------------------------------
  Purpose: 
    Notes: 
------------------------------------------------------------------------------*/
  DEFINE VARIABLE iAscValue     AS INTEGER    NO-UNDO.
  DEFINE VARIABLE iDivValue     AS INTEGER    NO-UNDO.
  DEFINE VARIABLE iModValue     AS INTEGER    NO-UNDO.
  DEFINE VARIABLE cValue        AS CHARACTER  NO-UNDO.


  iAscValue = ASC(ipcValue).

  iModValue = iAscValue MODULO 16.

  iDivValue = ( iAscValue - iModValue ) / 16.

  IF iModValue >= 10 THEN
    cValue = KEYLABEL(KEYCODE("A") + (iModValue MODULO 10) ).
  ELSE
    cValue = STRING(iModValue).

  RETURN "%" + STRING(iDivValue) + cValue.


END FUNCTION.

/* _UIB-CODE-BLOCK-END */
&ANALYZE-RESUME

This thread is closed