User:Strife Onizuka/int2hexdword

From Second Life Wiki
Jump to navigation Jump to search

I was thinking about something else when this crazy idea hit me on how to make a "better" int2hex function. It's really clever and should be faster in LSO (maybe even Mono). Anyway it was a bunch of fun to write. It works by shifting the bits around so that it can feed the integer to llIntegerToBase64 and have it come out hex. The divides "/" can be replaced with ">=" with no ill effect (except the code won't run in LSLEditor without an explicit typecast). This was really fun code to write.

<lsl> string int2hexdword(integer I) {

   //0xABCDEFGH
   integer A = (I >> 2) & 0x3C000000;//not an unsigned rshift
   integer B = (I & 0x0F000000) >>  4;
   integer C = (I & 0x00F00000) >>  6;
   integer D = (I & 0x000F0000) >>  8;
   integer E = (I & 0x0000F000) << 14;
   integer F = (I & 0x00000F00) << 12;
   integer G = (I & 0x000000F0) << 10;
   integer H = (I & 0x0000000F) <<  8;
   
   return llGetSubString(
           llInsertString(
               llIntegerToBase64(
                   A + B + C + D + 0xD34D3400
                   - (0xF8000000 * (A / 0x28000000))
                   - (0x03E00000 * (B / 0x00A00000))
                   - (0x000F8000 * (C / 0x00028000))
                   - (0x00003E00 * (D / 0x00000A00))
               ),
               4,
               llIntegerToBase64(
                   E + F + G + H + 0xD34D3400
                   - (0xF8000000 * (E / 0x28000000))
                   - (0x03E00000 * (F / 0x00A00000))
                   - (0x000F8000 * (G / 0x00028000))
                   - (0x00003E00 * (H / 0x00000A00))
               )
           ),
           0,
           7
       );

}

default {

   state_entry()
   {
       llSay(0, int2hexdword(0));
       llSay(0, int2hexdword(0x13579BDF));
       llSay(0, int2hexdword(0xFDB97531));
       llSay(0, int2hexdword(-1));
   }

} </lsl>