Help - Search - Members - Calendar
Full Version: Hex <-> Integer conversion
AC Tools Everything Macro > AC Tool Macros > Asheron's Call Macros
Triane
Here's an include file that provides the ability to convert back and forth between integers and hexadecimal numbers.

After INCLUDE'ng this code into your macro:
To convert a hex number to an integer:
CODE
Constants
 SomeHexnumber = 0x2eb
End

Call Hex2Int $SomeHexNumber

To convert an integer to a hex number:
CODE
Constants
 SomeIntNumber = 747
End

Call Int2Hex $SomeIntNumber

In all cases, the result is returned in a constant, $HexResult.

-Triane

[File Removed: There is an update posted two messages down (6 downloads)]
Starhawk
Triane,

Great code. 1 Problem though.

Here's what I'm trying to do:

CODE

Include includes/dec2hex.inc
SetActiveWindow Untitled - Notepad

Constants
tmp=
End

Keys ^a
delay 100
Keys {Del}
delay 100

saypaste Test start

Set tmp = 0xF
saypaste HEX Before $tmp
Call Hex2Int $tmp
saypaste Dec Before $hexresult
Compute tmp = $Hexresult + 1
saypaste Dec After $tmp
Call Int2Hex $tmp
SayPaste Hex After $Hexresult


saypaste Test stop



Also, for this to work right, I had to change the follwing line:
SetConst HexResult = $HexPrefix + Hex2Dec[Data]
to
SetConst HexResult = Hex2Dec[Data]

Which is what i THINK you meant for it to do anyway.

Starhawk
Even worse, set tmp = 0x10


INFINITE LOOP
Triane
Here's an update, it works just fine in both scenarios presented.

One important note: The Int2Hex procedure expects the integer to be an unsigned type, up to 40 bits in length. If you pass it a negative, signed integer, it probably will not return valid results (and may indeed break).

If your integers are signed values, you will have to convert them to unsigned before handing them to the routine. I cannot convert them because I have no way of knowing if it's a short integer (8 bit), an integer (16 bit), a long integer (32 bit), a double-long integer (64 bit) or something else entirely (40 bit for example).

To convert a signed integer to an unsigned integer, simply add the unsigned value of the largest bit in the integer type. For example, if you were using a long-integer, (32 bits in length) you would need to add 2^31 (or 2,147,483,648) to the negative value in order to generate an unsigned result.

The macro cannot handle double-longs as written (it maxes out at 40 bits). Nevertheless, you could try extending this simply by adding to the "Powers of 16" list. I didn't do this because I don't know whether or not, ACtool can handle double-long integers. So I stopped at 40-bits.

-Triane

PS: I do not know if the Hex2Int routine will return signed or unsigned values because that conversion is handled within ACTool's dataset routines and I don't know how Cam wrote it to behave.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.