Difference between revisions of "Hex"

From Second Life Wiki
Jump to navigation Jump to search
(→‎Implementation: faster without need for special condition handling)
(distinguish hex from hexu to conform to the cited Python spec -- add distinguishing test cases -- omit the confusing-to-newbie LSL-specific test-an-assignment-in-arg-list optimisation)
Line 21: Line 21:
<pre>
<pre>
// http://wiki.secondlife.com/wiki/Hex
// http://wiki.secondlife.com/wiki/Hex
string hexc = "0123456789ABCDEF";


string hex(integer value)
string HEXC = "0123456789ABCDEF";
 
string hexu(integer value)
{
{
     integer ny = (value & 0xF);
     integer ny = (value & 0xF);
     string nybbles = llGetSubString(hexc, ny, ny);
     string nybbles = llGetSubString(HEXC, ny, ny);
     if(value = (0xfffFFFF & (value >> 4)))
     value = (0xfffFFFF & (value >> 4));
    if (value)
    {
         do
         do
             nybbles = llGetSubString(hexc, ny = (value & 0xF), ny) + nybbles;
        {
        while(value = (value >> 4));
             ny = (value & 0xF);
            nybbles = llGetSubString(HEXC, ny, ny) + nybbles;
            value = (value >> 4);
        } while (value);
    }
     return "0x" + nybbles;
     return "0x" + nybbles;
}
string hex(integer value)
{
    if (value == (1 << 31)) return "-0x80000000";
    if (value < 0) return "-" + hex(-value);
    return llToLower(hexu(value));
}
}
</pre>
</pre>
Line 50: Line 64:
         llOwnerSay("Hello");
         llOwnerSay("Hello");
         llOwnerSay(hex(0));
         llOwnerSay(hex(0));
        llOwnerSay(hex(0x00FEDC00 & -0x00FEDC00));
        llOwnerSay(hex(1 << 30));
        llOwnerSay(hex(1 << 31));
        llOwnerSay(hex(0xFEDC9876));
         llOwnerSay(hex(-1));
         llOwnerSay(hex(-1));
         llOwnerSay(hex(1 << 30));
         llOwnerSay("OK");
         llOwnerSay(hex(-1 & ~-1));
          
         llOwnerSay("Hello again");
         llOwnerSay("Hello again");
         string item = llGetScriptName();
         string item = llGetScriptName();
Line 60: Line 78:
         llOwnerSay(hex(llGetInventoryPermMask(item, MASK_EVERYONE)));
         llOwnerSay(hex(llGetInventoryPermMask(item, MASK_EVERYONE)));
         llOwnerSay(hex(llGetInventoryPermMask(item, MASK_NEXT)));
         llOwnerSay(hex(llGetInventoryPermMask(item, MASK_NEXT)));
        llOwnerSay((string) llGetInventoryPermMask(item, MASK_NEXT));
         llOwnerSay("OK");
         llOwnerSay("OK");
     }
     }
Line 69: Line 88:
Hello
Hello
0x0
0x0
0xFFFFFFFF
0x400
0x40000000
0x40000000
-0x80000000
-0x123678a
-0x1
OK
Hello again
0x7fffffff
0x7fffffff
0x0
0x0
Hello again
0x7FFFFFFF
0x7FFFFFFF
0x0
0x0
0x8000
0x82000
0x8E000
532480
OK
OK
</pre>
</pre>

Revision as of 19:19, 8 October 2007

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 HEXC = "0123456789ABCDEF";

string hexu(integer value)
{
    integer ny = (value & 0xF);
    string nybbles = llGetSubString(HEXC, ny, ny);
    value = (0xfffFFFF & (value >> 4));
    if (value)
    {
        do
        {
            ny = (value & 0xF);
            nybbles = llGetSubString(HEXC, ny, ny) + nybbles;
            value = (value >> 4);
        } while (value);
    }
    return "0x" + nybbles;
}

string hex(integer value)
{
    if (value == (1 << 31)) return "-0x80000000";
    if (value < 0) return "-" + hex(-value);
    return llToLower(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));
        llOwnerSay(hex(0x00FEDC00 & -0x00FEDC00));
        llOwnerSay(hex(1 << 30));
        llOwnerSay(hex(1 << 31));
        llOwnerSay(hex(0xFEDC9876));
        llOwnerSay(hex(-1));
        llOwnerSay("OK");
        
        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((string) llGetInventoryPermMask(item, MASK_NEXT));
        llOwnerSay("OK");
    }
}

Sample results:

Hello
0x0
0x400
0x40000000
-0x80000000
-0x123678a
-0x1
OK
Hello again
0x7fffffff
0x7fffffff
0x0
0x0
0x82000
532480
OK