BinaryDecimalConverter

From Second Life Wiki
Revision as of 01:17, 18 October 2009 by Soundless Smalls (talk | contribs) (2 functions to do decimal/binary conversions)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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>