Answered Convert Integer to Hexadecimal Value

Cecil

19+ years progress programming and still learning.
I'm having a brain freeze and I thought I knew how to do this but I just can't think. How do I convert an Integer into a Hexadecimal value?

This is how I thought you can do it:
Code:
DEFINE VARIABLE r_RAW AS RAW       NO-UNDO.
PUT-UNSIGNED-SHORT (r_RAW,1) = 52203.
MESSAGE CAPS(STRING(HEX-ENCODE(r_RAW))).

But 52203 becomes EBCE, but what I was expecting was CBEB

gw002-brainfreeze.jpg
 

Cringer

ProgressTalk.com Moderator
Staff member
There's this from the Peg:
Code:
FUNCTION dec2hex RETURNS character
   ( INPUT iNumber as integer ) :

    def var iRem as integer no-undo.
    def var cResult as char no-undo.

    if iNumber eq 0 then return "0".
    else if iNumber lt 0 then return error.

    do while iNumber gt 0:
        assign
            iRem = iNumber modulo 16
            iNumber = truncate(iNumber / 16,0)
            cResult = if iRem = 0 then "0" + cResult
                      else entry(iRem,"1,2,3,4,5,6,7,8,9,a,b,c,d,e,f") +
cResult.
    end.
    return cResult.
end.

message dec2hex(52203).
 

Cecil

19+ years progress programming and still learning.
Thanks for that. That's what I was looking for. I could not find it because I was googling for inttohex and int2hex. I thought I saw a more elegant solution recently but thinking about it more it was probably hex2dec.

I think I might be able to optimise the above code a little, probably won't make it any faster.
 

Cecil

19+ years progress programming and still learning.
Found the wrong code, hex2int just in case somebody wanted a hex2int function.

image.jpg
 

Stefan

Well-Known Member
Have an intToHex:

Code:
FUNCTION intToHex RETURNS CHARACTER (
   i_iint AS INT64
):

   DEF VAR chex  AS CHAR NO-UNDO.
   DEF VAR rbyte AS RAW  NO-UNDO.


   DO WHILE i_iint > 0:

      PUT-BYTE( rbyte, 1 ) = i_iint MODULO 256.
      chex = STRING( HEX-ENCODE( rbyte ) ) + chex.
      i_iint = TRUNCATE( i_iint / 256, 0 ).

   END.

   RETURN chex.

END FUNCTION.

MESSAGE intToHex( 52203 ) VIEW-AS ALERT-BOX.
 

Cecil

19+ years progress programming and still learning.
Have an intToHex:

Code:
FUNCTION intToHex RETURNS CHARACTER (
   i_iint AS INT64
):
   DEF VAR chex  AS CHAR NO-UNDO.
   DEF VAR rbyte AS RAW  NO-UNDO.


   DO WHILE i_iint > 0:

      PUT-BYTE( rbyte, 1 ) = i_iint MODULO 256.
      chex = STRING( HEX-ENCODE( rbyte ) ) + chex.
      i_iint = TRUNCATE( i_iint / 256, 0 ).

   END.

   RETURN chex.

END FUNCTION.

MESSAGE intToHex( 52203 ) VIEW-AS ALERT-BOX.

Thanks, that's what I was originally looking for. That looks better elegant coding.
 
Last edited:

Cecil

19+ years progress programming and still learning.
Dam it all. This HEX stuff keeps byting (play on word) me on the ass. What do I need to do so the intToHex() function can handle negative numbers? i.e. -1 becomes 0xFFFFFFFF.
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
For starters, you might want to pass into the function the intended length (in digits/nybbles) of the output.
As a qword (64 bits), -1 (decimal) = 0xFFFFFFFFFFFFFFFF.
As a dword (32 bits), -1 = 0xFFFFFFFF.
As a word (16 bits), -1 = 0xFFFF.
As a byte (8 bits), -1 = 0xFF.
 

Cecil

19+ years progress programming and still learning.
For starters, you might want to pass into the function the intended length (in digits/nybbles) of the output.
As a qword (64 bits), -1 (decimal) = 0xFFFFFFFFFFFFFFFF.
As a dword (32 bits), -1 = 0xFFFFFFFF.
As a word (16 bits), -1 = 0xFFFF.
As a byte (8 bits), -1 = 0xFF.

Ok, Just for now I'm working with exclusively just 32bit (word). What need to change?
 

Rob Fitzpatrick

ProgressTalk.com Sponsor
How about this:

Code:
FUNCTION intToHex RETURNS CHARACTER (
   i_iint AS INT64
):
   DEF VAR chex  AS CHAR NO-UNDO.
   DEF VAR rbyte AS RAW  NO-UNDO.

   if i_int = 0 then return "0".
   if i_iint < 0 then i_iint = 4294967296 + i_iint.
 
   DO WHILE i_iint > 0:
      PUT-BYTE( rbyte, 1 ) = i_iint MODULO 256.
      chex = STRING( HEX-ENCODE( rbyte ) ) + chex.
      i_iint = TRUNCATE( i_iint / 256, 0 ).
   END.

   RETURN chex.

END FUNCTION.

MESSAGE intToHex( -1 ) VIEW-AS ALERT-BOX.

Minimal testing ;). Also handles 0, returning "0" instead of "".
 

Cecil

19+ years progress programming and still learning.
How about this:

Code:
FUNCTION intToHex RETURNS CHARACTER (
   i_iint AS INT64
):
   DEF VAR chex  AS CHAR NO-UNDO.
   DEF VAR rbyte AS RAW  NO-UNDO.

   if i_int = 0 then return "0".
   if i_iint < 0 then i_iint = 4294967296 + i_iint.

   DO WHILE i_iint > 0:
      PUT-BYTE( rbyte, 1 ) = i_iint MODULO 256.
      chex = STRING( HEX-ENCODE( rbyte ) ) + chex.
      i_iint = TRUNCATE( i_iint / 256, 0 ).
   END.

   RETURN chex.

END FUNCTION.

MESSAGE intToHex( -1 ) VIEW-AS ALERT-BOX.

Minimal testing ;). Also handles 0, returning "0" instead of "".

Thanks for that. I've made a minor tweak to the code and made it into a static class object. Better than having include file with bunch of functions.
Also I don't know why but I find myself compelled to use hexadecimal representation of decimal values, hence the reason why I've replaced 4294967296 with 0x100000000. Maybe because it looks a little more pleasing to the eye and also I see constant values written as HEX in other programming languages as well, JAVA, Python, C# etc.

src.common.BitWise.cls
Code:
CLASS src.common.BitWise:

    METHOD STATIC CHARACTER intToHex (i_iint AS INT64):

       DEF VAR chex  AS CHAR NO-UNDO.
       DEF VAR rbyte AS RAW  NO-UNDO.

       IF i_iint = 0 THEN RETURN "0".
       if i_iint < 0 THEN i_iint = 0x100000000 + i_iint.

       DO WHILE i_iint > 0:
          PUT-BYTE( rbyte, 1 ) = i_iint MODULO 0x100.
          chex = STRING( HEX-ENCODE( rbyte ) ) + chex.
          i_iint = TRUNCATE( i_iint / 0x100, 0 ).
       END.

       RETURN CAPS(chex).

   END METHOD.

END CLASS.

BitWiseTestCode.p:
Code:
DEFINE VARIABLE in32BITINT AS INTEGER     NO-UNDO.
DEFINE VARIABLE in64BITINT AS INT64       NO-UNDO.

ASSIGN
    in32BITINT = -1
    in64BITINT = -1.

MESSAGE
    src.common.BitWise:intToHex(in32BITINT) SKIP
    src.common.BitWise:intToHex(in64BITINT) SKIP
    .


I do wish there was a method of reloading/refreshing/flushing static methods, It can get quite frustrating during development.
 
Last edited:

Stefan

Well-Known Member
A method with an int64 signature and then treating the input as a 32-bit signed integer is just wrong.
 

tamhas

ProgressTalk.com Sponsor
If you use PDSOE and set it to use a different AVM for run than is used for editing, then the refresh issue goes away.
 

tamhas

ProgressTalk.com Sponsor
No, this was in response to:

I do wish there was a method of reloading/refreshing/flushing static methods, It can get quite frustrating during development.
 
Top