Integer2Binary

From Second Life Wiki
Revision as of 07:08, 20 October 2010 by Strife Onizuka (talk | contribs) (Created page with 'Code for converting integers to and from binary. Big and little endian versions provided. Obsolete code can be found here: [http://forums-archive.secondlife.com/15/e0/12886/1.ht...')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to 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>