Hi Folks, is there a ABL function to convert integer to hex that I am not finding?
Thanks.
Not that I know of, no.
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
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.
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.
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.
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.
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.
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.
Instead of worrying about byte order in a long, you can also just use bytes:
Thank you all for your remarks.
We are on Windows so tbergman solution is the one. Spot on. Thank you.
Hex = System.String:Format("~{0:X}", intValue)