integer to hex

Posted by bremmeyr on 13-Sep-2016 14:54

Hi Folks, is there a ABL function to convert integer to hex that I am not finding? 

Thanks. 

All Replies

Posted by James Palmer on 13-Sep-2016 16:13

Not that I know of, no.

Posted by Alon Blich on 13-Sep-2016 16:19

you might want to try the math_int2hex() and math_hex2int() functions in the slibmath.p, in the standard libraries project at oehive.org.

<code>

{slib/slibmath.i}

message math_int2hex( 1000 ) math_hex2int( "a" ).

</code>

hth

Posted by tbergman on 13-Sep-2016 16:26

If you're on Windows .Net makes it easy.

METHOD PUBLIC CHARACTER IntToHex (INPUT vInt AS Int64):
  Return System.Convert:ToString(vInt, 16).
END METHOD.



and the other way

METHOD PUBLIC INT64 HexToINT (INPUT pHex AS CHARACTER):
  RETURN System.Convert:ToInt64(pHex, 16).
END METHOD.

Posted by Richard.Kelters on 14-Sep-2016 04:30

In OpenEdge you might be looking for something like this:

DEFINE VARIABLE rw AS RAW       NO-UNDO.

DEFINE VARIABLE it AS INTEGER     NO-UNDO.

DEFINE VARIABLE ch AS CHARACTER   NO-UNDO.

it = 6541.

PUT-LONG(rw,1) = it.

ch = HEX-ENCODE(rw).

MESSAGE ch

 VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by Pieterm on 14-Sep-2016 05:12

Richard, The code returns the HEX backwards, or the issue is rather that for hex the value has to be build up from the last to first remainders.

Just by change I needed the same thing yesterday and after investigation the formula, I wrote the snippet below.

It is basic but it works.

DEF VAR iInt AS INTE.

DEF VAR cHex AS CHAR.

DEF VAR iMod AS INTE.

ASSIGN iInt = 7722.

REPEAT:

iMod = iInt MODULO 16.

IF iMod = 0 THEN LEAVE.

cHex = ENTRY(iMod,"1,2,3,4,5,6,7,8,9,A,B,C,D,E,F",",") + cHex.

iInt = INTEGER(ENTRY(1,STRING(iInt / 16),".")).

END.

MESSAGE cHex VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by Richard.Kelters on 14-Sep-2016 06:14

That's because OpenEdge byte order is Little-endian  and you want Big-endian. You could do this to sove that:

DEFINE VARIABLE rw AS MEMPTR       NO-UNDO.

DEFINE VARIABLE it AS INTEGER     NO-UNDO.

DEFINE VARIABLE ch AS CHARACTER   NO-UNDO.

SET-BYTE-ORDER( rw ) = 2.

SET-SIZE(rw) = 4.

it = 705.

PUT-LONG(rw,1) = it.

ch = HEX-ENCODE(rw).

MESSAGE ch

 VIEW-AS ALERT-BOX INFO BUTTONS OK.

Posted by bronco on 14-Sep-2016 06:19

This code is not portable. It's not OpenEdge which is little or big endian, it's the underlying CPU. So running on PowerPC of x86/x64 give different results.

Posted by Richard.Kelters on 14-Sep-2016 06:35

The code is portable because explicitly setting the byte-order to 2 makes it big-endian according to the help.

My remark about OpenEdge was a mistake, sorry about that.

Posted by Stefan Drissen on 14-Sep-2016 06:47

Instead of worrying about byte order in a long, you can also just use bytes:

www.progresstalk.com/.../

Posted by bremmeyr on 14-Sep-2016 07:09

Thank you all for your remarks.

Posted by bremmeyr on 14-Sep-2016 15:15

We are on Windows so  tbergman solution is the one.  Spot on. Thank you.

Posted by jquerijero on 27-Sep-2016 16:25

Hex = System.String:Format("~{0:X}", intValue)

This thread is closed