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). By changing the multiplied constants, you can get it produce lowercase characters.

<lsl>string int2hexdword(integer I) {

   //ABCDEFGH
   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))//lowercase=0x90000000, uppercase=0xF8000000
                   - (0x03E00000 * (B / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                   - (0x000F8000 * (C / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                   - (0x00003E00 * (D / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
               ),
               4,
               llIntegerToBase64(
                   E + F + G + H + 0xD34D3400
                   - (0xF8000000 * (E / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000
                   - (0x03E00000 * (F / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                   - (0x000F8000 * (G / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                   - (0x00003E00 * (H / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
               )
           ),
           0,
           7
       );

}

default {

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

}</lsl>

For fun I wrote a version that includes the "0x" header. The first version of the script (above) uses 4 characters from each llIntegerToBase64 conversion. This version uses 5 characters. To change the case of the "x" see the comments.

<lsl>string int2hexdword(integer I) {

   //0xABCDEFGH
   integer A = (I >> 14) & 0x0003C000;//not an unsigned rshift
   integer B = (I & 0x0F000000) >> 16;
   integer C = (I & 0x00F00000) >> 18;
   integer D = (I & 0x000F0000) << 10;
   integer E = (I & 0x0000F000) <<  8;
   integer F = (I & 0x00000F00) <<  6;
   integer G = (I & 0x000000F0) <<  4;
   integer H = (I & 0x0000000F) <<  2;

   return llGetSubString(
           llInsertString(
               llIntegerToBase64(
                   A + B + C + 0xD31D34D3//lowercase-x=0xD31D34D3, uppercase-X=0xD17D34D3
                   - (0x000F8000 * (A / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                   - (0x00003E00 * (B / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                   - (0x000000F8 * (C / 0x00000028))//lowercase=0x00000090, uppercase=0x000000F8
               ),
               5,
               llIntegerToBase64(
                   D + E + F + G + H + 0xD34D34D3
                   - (0xF8000000 * (D / 0x28000000))//lowercase=0x90000000, uppercase=0xF8000000
                   - (0x03E00000 * (E / 0x00A00000))//lowercase=0x02400000, uppercase=0x03E00000
                   - (0x000F8000 * (F / 0x00028000))//lowercase=0x00090000, uppercase=0x000F8000
                   - (0x00003E00 * (G / 0x00000A00))//lowercase=0x00002400, uppercase=0x00003E00
                   - (0x000000F8 * (H / 0x00000028))//lowercase=0x00000090, uppercase=0x000000F8
               )
           ),
           0,
           9
       );

}</lsl>