IntToHexString

From NWN Lexicon
Jump to: navigation, search



IntToHexString(int)

This function converts an int to a string representation of the same hexadecimal value.

string IntToHexString(
    int nInteger
);

Parameters

nInteger

The integer to convert to hexadecimal (base 16).


Description

This function coverts an integer to a hexadecimal string representation of the same value, so that converting 123 to a hexString would yield "0x0000007b".



Remarks

The main use of this is for more easily debugging bitwise operations on integers. Hexidecimal can define values for bitwise operations more easily. See int and Bitwise Operators for more inforamtion.


Version

1.30

Example

int foo = 0x00003000;
string bar =  IntToHexString(foo); // String is "0x00003000"
string bar2 = IntToString(foot);   // String is "12288"

To convert a string hexidecimal value, such as from the baseitems.2da column "EquipableSlots" into a usable int for bitmask tests, clippy has helpfully written this function:

// Turns 0x1C000 into a usable integer value "114688" (or 0x0001C000 if using IntToHexString()) for bitmask tests.
int HexStringToInt(string s)
{
    s = GetStringLowerCase(s);
    int res = 0;
    int len = GetStringLength(s);
    int i;
    for (i = len - 1; i >= 0; i--) {
        int n = FindSubString("0123456789abcdef", GetSubString(s, i, 1));
        if (n == -1)
            return res;
        res |= n << ((len - i -1)*4);
    }
    return res;
}
 
const int EQUIPPABLE_SLOT_MAIN_HAND          = 0x00010;// Bitmask for Main Hand
const int EQUIPPABLE_SLOT_OFF_HAND           = 0x00020;// Bitmask for Off Hand
 
void main()
{
    // Lookup the equippable slot for nBaseItemType defined
    int nBaseItemType = BASE_ITEM_SLING;
    int nLookup = HexStringToInt(Get2DAString("baseitems","EquipableSlots", nBaseItemType));
 
    // Test, can this be equipped in the off or main hand?
    if(nLookup & EQUIPPABLE_SLOT_MAIN_HAND ||
       nLookup & EQUIPPABLE_SLOT_OFF_HAND)
    {
        SpeakString("A sling can be equipped in either main or off hand, or possibly both!");
    }
}

See Also

functions: 

IntToString



 author: Charles Feduke, editor: Lilac Soul, additional contributor(s): Lilac Soul