Difference between revisions of "BinaryDecimalConverter"

From Second Life Wiki
Jump to navigation Jump to search
Line 19: Line 19:


<lsl>
<lsl>
string intToBin(integer val)
string decToBin(integer val)
{
{
     string binary;
     string binary;

Revision as of 05:34, 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 decToBin(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 :)