Difference between revisions of "Hex"

From Second Life Wiki
Jump to navigation Jump to search
(Caution: This page is a work in progress. The specification, the implementation, the demo, and the sample results may not yet be totally consistent. See the discussion tab.)
(try to capture all the Talk:Hex up thru now)
Line 1: Line 1:
{{LSL Header}}__NOTOC__
{{LSL Header}}__NOTOC__
<div id="box">
<div id="box">
{{#vardefine:p_value_desc|value to be converted}}
{{#vardefine:p_value_desc|signed value to be expressed as signed hex}}
== Function: [[string]] hex([[integer]] {{LSL Param|value}}); ==
== Function: [[string]] hex([[integer]] {{LSL Param|value}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
'''Caution: This page is a work in progress. The specification, the implementation, the demo, and the sample results may not yet be totally consistent. See the [[Talk:Hex|discussion]] tab.'''


Returns the hexadecimal nybbles of the signed integer '''{{LSL Param|value}}''', in order, prefixed by "0x" or by "-0x", without any leading zeroes.
Returns the hexadecimal nybbles of the signed integer value in order. Specifically returns the nybbles from most to least significant, starting with the first nonzero nybble, folding every nybble to lower case, and beginning with the nonnegative "0x" or the negative "-0x" prefix.


Parameters:
Parameters:
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.
Note: Results with eight nybbles begin always with one of the positive octal nybbles 1 2 3 4 5 6 7, never with a zero or non-octal nybble 0 8 9 a b c d e f.
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.
Caution: This page was a work in progress as of 2007-10. The specification, the implementations, the demo, and the sample results may not yet be totally consistent.
</div></div>
</div>
</div>


<div id="box">
<div id="box">
== Implementation ==
== Implementations ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
=== Easy To Use, and Correct At A Glance ===
You should find this implementation feels easy to call and modify and review. Please consider sharing your experience in the [[Talk:Hex|discussion]] tab.
<pre>
<pre>
// http://wiki.secondlife.com/wiki/Hex
FIXME
</pre>


string HEXC = "0123456789ABCDEF";
=== Fast ===


string hexu(integer value)
You should agree this implementation feels like it could run fast in 2007 SL before Mono. Please consider contributing measures of how much faster/ smaller this code is to the [[Talk:Hex|discussion]] tab.
{
    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)
<pre>
{
FIXME
    if (value < 0) return "-" + hexu(-value);
    return hexu(value);
}
</pre>
</pre>
</div></div>
 
=== Small ===
 
You should agree that reworking the correct-at-a-glance implementation to make the code run faster feels like making the LSL compiler produce more byte code. Please consider contributing smaller code here.
 
</div>
</div>


<div id="box">
<div id="box">
== Demo ==
== Demo ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Print the most astonishing test cases and then also the permission masks of a script.
Print the most astonishing test cases and then also the permission masks of a script.


Code:
Code:
<pre>
<pre>
default
FIXME
{
    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");
    }
}
</pre>
</pre>


Sample results:
Sample Results:
<pre>
<pre>
Hello
FIXME
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
</pre>
</pre>
</div></div>
 
</div>
</div>


<div id="box">
<div id="box">
== Specification By Consensus ==
<div style="padding: 0.5em">
We programmers divide into schools by our passionately held personal aesthetics, just like other poets. Not everyone here agrees exactly on the relative measures and importance of such fuzzy source code qualities as:
# easy to use
# correct at a glance
# small
# fast


The implementations we present here work exactly like the hex function of the Python scripting language, doc'ed deep within http://docs.python.org/lib/built-in-funcs.html after such disputes over its detailed specification as http://www.python.org/dev/peps/pep-0237/
Specifically, these implementations:
# return lower case a b c d e f rather than upper case A B C D E F,
# return a signed 31-bit result if negative, rather than an unsigned 32-bit result, and
# omit the leading quads of zeroed bits, except returns "0x0" rather than "0x" when the result is zero.
In this way, these implementations reproduce how hex integer literals often appear in LSL script, conforming to such traditional AT&T C conventions as:
# Return a meaningless "0" before the "x",  as LSL and C compilers require.
# Return the "x" on the left as in LSL and C, not the "h" on the right as in Assembly code.
# Return the nybbles listed from most to least significant as in English, not listed from least to most significant as in Arabic.
As you read this page, you have to wade thru more than one implementation only because not all our community yet agrees that the only code you wish to see is the code from my school.
</div>
</div>
<div id="box">
== See Also ==
== See Also ==
<div style="padding: 0.5em">
<div style="padding: 0.5em">
'''Functions'''
'''Functions'''
* [[llGetInventoryPermMask]]
* [[llGetInventoryPermMask]]
* [[llGetObjectPermMask]]
* [[llGetObjectPermMask]]
* [[llIntegerToBase64]]
* [[llMessageLinked]]
* [[llList2Integer]]
* [[llList2Integer]]
* [[llList2String]]
* [[llList2String]]
</div></div>
 
</div>
</div>


{{LSLC|Examples|Hex}}
{{LSLC|Examples|Hex}}

Revision as of 07:47, 11 October 2007

Function: string hex(integer value);

Returns the hexadecimal nybbles of the signed integer value in order. Specifically returns the nybbles from most to least significant, starting with the first nonzero nybble, folding every nybble to lower case, and beginning with the nonnegative "0x" or the negative "-0x" prefix.

Parameters:

• integer value signed value to be expressed as signed hex

Note: Results with eight nybbles begin always with one of the positive octal nybbles 1 2 3 4 5 6 7, never with a zero or non-octal nybble 0 8 9 a b c d e f.

Caution: This page was a work in progress as of 2007-10. The specification, the implementations, the demo, and the sample results may not yet be totally consistent.

Implementations

Easy To Use, and Correct At A Glance

You should find this implementation feels easy to call and modify and review. Please consider sharing your experience in the discussion tab.

FIXME

Fast

You should agree this implementation feels like it could run fast in 2007 SL before Mono. Please consider contributing measures of how much faster/ smaller this code is to the discussion tab.

FIXME

Small

You should agree that reworking the correct-at-a-glance implementation to make the code run faster feels like making the LSL compiler produce more byte code. Please consider contributing smaller code here.

Demo

Print the most astonishing test cases and then also the permission masks of a script.

Code:

FIXME

Sample Results:

FIXME

Specification By Consensus

We programmers divide into schools by our passionately held personal aesthetics, just like other poets. Not everyone here agrees exactly on the relative measures and importance of such fuzzy source code qualities as:

  1. easy to use
  2. correct at a glance
  3. small
  4. fast

The implementations we present here work exactly like the hex function of the Python scripting language, doc'ed deep within http://docs.python.org/lib/built-in-funcs.html after such disputes over its detailed specification as http://www.python.org/dev/peps/pep-0237/

Specifically, these implementations:

  1. return lower case a b c d e f rather than upper case A B C D E F,
  2. return a signed 31-bit result if negative, rather than an unsigned 32-bit result, and
  3. omit the leading quads of zeroed bits, except returns "0x0" rather than "0x" when the result is zero.

In this way, these implementations reproduce how hex integer literals often appear in LSL script, conforming to such traditional AT&T C conventions as:

  1. Return a meaningless "0" before the "x", as LSL and C compilers require.
  2. Return the "x" on the left as in LSL and C, not the "h" on the right as in Assembly code.
  3. Return the nybbles listed from most to least significant as in English, not listed from least to most significant as in Arabic.

As you read this page, you have to wade thru more than one implementation only because not all our community yet agrees that the only code you wish to see is the code from my school.