User:Strife Onizuka/hexchars

From Second Life Wiki
< User:Strife Onizuka
Revision as of 17:49, 8 November 2009 by Strife Onizuka (talk | contribs) (Created page with 'A quick and dirty function for calculating the number of characters needed to represent an integer in hex (assuming the unsigned notation). The only difference between this and l...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

A quick and dirty function for calculating the number of characters needed to represent an integer in hex (assuming the unsigned notation). The only difference between this and llCeil(log16(a)) is that this returns 1 for zero and it handles negative numbers. <lsl>integer hexchars(integer a) {

   //the OR 15 handles 0 & 1 (which falsely go to zero)
   //the OR shift and AND deal with negative numbers
   //the long float is ln(16)
   return llCeil(llLog((a | 15 | (a >> 1)) & 0x7FFFFFFF) / 2.7725887222397812376689284858327);

}

default {

   state_entry()
   {
       list a = [-1, 0x0, 0x1, 2,
           0xF,0x10, 0x11,
           0xFF,0x100, 0x101,
           0xFFF,0x1000, 0x1001,
           0xFFFF,0x10000, 0x10001,
           0xFFFFF,0x100000, 0x100001,
           0xFFFFFF,0x1000000, 0x1000001,
           0xFFFFFFF,0x10000000, 0x10000001,
           0x80000000, 0xFFFFFFFE];
       integer b = -llGetListLength(a);
       for(;b;++b)
           llOwnerSay(llList2CSV([llList2Integer(a,b), hexchars(llList2Integer(a,b))]));
   }

}</lsl>