Difference between revisions of "BinaryDecimalConverter"

From Second Life Wiki
Jump to navigation Jump to search
(2 functions to do decimal/binary conversions)
 
Line 35: Line 35:
}
}
</lsl>
</lsl>
Greets from Soundless :)

Revision as of 02:21, 18 October 2009

The next function converts a binary value to a decimal number. Works +/- 5 times faster than Base2Dec : <lsl> integer binToDec(string val) {

   integer dec;
   
   integer i = -llStringLength(val);
   
   while(++i < 0)
   {
       dec = (dec << 1) + (integer)llGetSubString(val, i, i);
   }
   return dec;

} </lsl>

This one converts a decimal to a binary value:

<lsl> string intToBin(integer val) {

   string binary;
   integer i = val;
   while (i)
   {
       if (i % 2)
           binary = "1" + binary;
       else
           binary = "0" + binary;
       
       i = i >> 1;
   }
   return binary;

} </lsl>

Greets from Soundless :)