Integer2Binary

From Second Life Wiki
Jump to: navigation, search

Code for converting integers to and from binary. Big and little endian versions provided.

Obsolete code can be found here: [1]

Big Endian

<lsl>string integer2binary(integer a) {//big endian

   integer c = 1;
   string d = "";

do

       d = (string)((a&c)!=0) + d

while(a & -(c = c << 1));

   return d;

}

integer binary2integer(string a) {//big endian

   integer b = 0;
   integer c = llStringLength(a);
   integer d = 0;
   while(b < c)
       d = d | ((integer)llGetSubString(a, b, b++) << ~(b - c));
   return d;

}</lsl>

Little Endian

<lsl>string integer2binary(integer a) {//little endian

   integer c = 1;
   string d = "";

do

       d += (string)((a&c)!=0);

while(a & -(c = c << 1));

   return d;

}

integer binary2integer(string a) {//little endian

   integer b = 0;
   integer c = llStringLength(a);
   integer d = 0;
   while(b < c)
       d = d | ((integer)llGetSubString(a, b, b++) << b);
   return d;

}</lsl>