Consultor Eletrônico



Kbase P19273: How to convert an INTEGER to hexadecimal representation?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   2/19/2008
Status: Unverified

GOAL:

How to convert an INTEGER to hexadecimal representation?

FIX:

The following 4GL code will take any integer and converted to hexadecimal representation.

FUNCTION int2hex RETURNS CHARACTER
( vi AS INTEGER ):
DEF VAR hexBit AS CHARACTER FORMAT "x(1)" EXTENT 16 INIT
['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'].

IF vi < 16 THEN RETURN hexbit[vi + 1].

RETURN
int2hex( integer( TRUNCATE( vi / 16, 0 ) ) )
+ hexbit[ ( vi MODULO 16 ) + 1 ] .
END FUNCTION.

FUNCTION int2hexchar RETURNS CHARACTER
( vi AS INTEGER ):
DEF VAR chex AS CHARACTER.
chex = int2hex( vi ).
RETURN '0x' + FILL( '0', 8 - LENGTH( chex ) ) + chex.
END FUNCTION.

DEF VAR vi AS INTEGER.
vi = 123456.
MESSAGE int2hexchar( vi ) VIEW-AS ALERT-BOX.
/* message displays 0x0001e240 */

/* here is a method to convert a RECID to ROWID */
DEF VAR vrROW AS ROWID.
DEF VAR vrREC AS RECID.

FIND FIRST customer NO-LOCK NO-ERROR.
vrREC = RECID( customer ).
vrROW = TO-ROWID( int2hexchar( INTEGER( vrREC ) ) ) .

/* vrROW is the same as ROWID( customer ) */
MESSAGE vrROW = ROWID( customer ) VIEW-AS ALERT-BOX.
/* message displays YES */
/* non recursive algorithm */
FUNCTION Dec2Hex returns character ( pi as integer ) :
def var hNum as character extent 16 initial
[ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E' ].
def var chex as character.
def var pres as integer.
def var pmod as integer.
pres = pi.
do while pres > 0:
pmod = ( pres modulo 16 ).
chex = hNum[pmod + 1] + chex.
pres = ( pres - pmod ) / 16.
END.
return chex.
END.