Hex
Revision as of 18:49, 7 October 2007 by Ppaatt Lynagh (talk | contribs) (convert integer to signed lower hex without leading zeroes, a la Python)
| 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. and this function returns the easier-to-type lower case nybbles a la AT&T, rather than the easier-to-read upper case nybbles a la IBM.
Implementation
// http://wiki.secondlife.com/wiki/Hex
string hex(integer value)
{
if (value == 0) return "0x0";
if (value == (1 << 31)) { return "-0x80000000"; }
string nybbles = "";
while (value)
{
integer ny = (value & 0xF);
nybbles = llGetSubString("0123456789ABCDEF", ny, ny) + nybbles;
value = (value >> 4);
value = (value & 0xfffFFFF);
}
return "0x" + nybbles;
}
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));
llOwnerSay(hex(-1));
llOwnerSay(hex(1 << 30));
llOwnerSay(hex(-1 & ~-1));
llOwnerSay("Hello again");
string item = llGetScriptName();
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_BASE)));
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_OWNER)));
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_GROUP)));
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_EVERYONE)));
llOwnerSay(hex(llGetInventoryPermMask(item, MASK_NEXT)));
llOwnerSay("OK");
}
}
Sample results:
Hello 0x0 0xFFFFFFFF 0x40000000 0x0 Hello again 0x7FFFFFFF 0x7FFFFFFF 0x0 0x8000 0x8E000 OK