Difference between revisions of "IntToHexString"

From NWN Lexicon
Jump to: navigation, search
(Example)
 
(2 intermediate revisions by 2 users not shown)
Line 8: Line 8:
  
 
<nwscript>
 
<nwscript>
[[string | string ]] IntToHexString(
+
string IntToHexString(
    [[int | int ]] nInteger
+
    int nInteger
);
+
);
 
</nwscript>
 
</nwscript>
  
Line 23: Line 23:
 
====Description====
 
====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”.
+
This function coverts an integer to a hexadecimal string representation of the same value, so that converting 123 to a hexString would yield "0x0000007b".
  
 
<br /><br />
 
<br /><br />
Line 29: Line 29:
 
====Remarks====
 
====Remarks====
  
If the first numeric system you learned was hexadecimal then these values would be useful for you. For the rest of us normal types stick to the basic string representation.
+
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.
  
 
<br />
 
<br />
Line 40: Line 40:
  
 
<nwscript>
 
<nwscript>
int foo = 300;
+
int foo = 0x00003000;
string bar =  IntToHexString(foo);
+
string bar =  IntToHexString(foo); // String is "0x00003000"
 +
string bar2 = IntToString(foot);  // String is "12288"
 +
</nwscript>
 +
 
 +
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:
 +
 
 +
<nwscript>
 +
// 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!");
 +
    }
 +
}
 
</nwscript>
 
</nwscript>
  

Latest revision as of 00:17, 8 June 2020



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