Hex: Difference between revisions
Jump to navigation
Jump to search
→Implementation: it's a speed thing, if your going to pull them out, you might as well use a regular while loop; the LSL compile does not optimize bytecode. |
Changing nybble case at runtime wastes CPU time. Recursion should be avoided like the plague, it's very expensive in LSL; anyway that case doesn't need special handling |
||
| Line 13: | Line 13: | ||
This function works like the hex function of the Python scripting language, doc'ed within http://docs.python.org/lib/built-in-funcs.html. | This function works like the hex function of the Python scripting language, doc'ed within http://docs.python.org/lib/built-in-funcs.html. | ||
Specifically, this function returns a signed 31-bit result if negative, rather than a 32-bit result. | Specifically, this function returns a signed 31-bit result if negative, rather than a 32-bit result. | ||
Returns upper case nybbles by default but that can easily be changed by editing the value of HEXC. | |||
</div></div> | </div></div> | ||
| Line 40: | Line 40: | ||
string hex(integer value) | string hex(integer value) | ||
{ | { | ||
if (value < 0) return "-" + hexu(-value); | |||
if (value < 0) return "-" + | return hexu(value); | ||
return | |||
} | } | ||
</pre> | </pre> | ||
Revision as of 09:54, 10 October 2007
| LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Function: string hex(integer value);
Returns the hexadecimal nybbles of the signed integer value, in order, prefixed by "0x" or by "-0x", without any leading zeroes.
Parameters:
| • integer | value | – | value to be converted |
This function works like the hex function of the Python scripting language, doc'ed within http://docs.python.org/lib/built-in-funcs.html. Specifically, this function returns a signed 31-bit result if negative, rather than a 32-bit result. Returns upper case nybbles by default but that can easily be changed by editing the value of HEXC.
Implementation
// http://wiki.secondlife.com/wiki/Hex
string HEXC = "0123456789ABCDEF";
string hexu(integer value)
{
integer ny = (value & 0xF);
string nybbles = llGetSubString(HEXC, ny, ny);
if ((value = (0xfffFFFF & (value >> 4))))
{
do
{
nybbles = llGetSubString(HEXC, ny = (value & 0xF), ny) + nybbles;
} while ((value = (value >> 4)));
}
return "0x" + nybbles;
}
string hex(integer value)
{
if (value < 0) return "-" + hexu(-value);
return hexu(value);
}
Demo
Print the most astonishing test cases and then also the permission masks of a script.
Code:
default
{
state_entry()
{
llOwnerSay("Hello");
llOwnerSay(hex(0) + " == 0");
llOwnerSay(hex(0x00FEDC00 & -0x00FEDC00) + " == (0x00FEDC00 & -0x00FEDC00)");
llOwnerSay(hex(1 << 30) + " == (1 << 30)");
llOwnerSay(hex(1 << 31) + " == (1 << 31)");
llOwnerSay(hex(0xFEDC9876) + " == 0xFEDC9876");
llOwnerSay(hex(-1) + " == -1");
llOwnerSay(hex(0x123456789) + " == 0x123456789");
llOwnerSay("OK");
llOwnerSay("Hello again");
string item = llGetScriptName();
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_BASE)) + " as base");
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_OWNER)) + " by owner");
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_GROUP)) + " by group");
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_EVERYONE)) + " by anyone");
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_NEXT)) + " by next owner");
llOwnerSay((string) llGetInventoryPermMask(item, MASK_NEXT) + " by next owner");
llOwnerSay("OK");
}
}
Sample results:
Hello 0x0 == 0 0x400 == (0x00FEDC00 & -0x00FEDC00) 0x40000000 == (1 << 30) -0x80000000 == (1 << 31) -0x123678a == 0xFEDC9876 -0x1 == -1 -0x1 == 0x123456789 OK Hello again 0x7fffffff as base 0x7fffffff by owner 0x0 by group 0x0 by anyone 0x82000 by next owner 532480 by next owner OK