Integer2Binary
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...')
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>