Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Library: Bin2Dec2Bin

ArrayTo String
Registered User
Join date: 24 Jun 2008
Posts: 1
06-25-2008 17:08
hello
this script contains useful functions to convert from decimal to binary and binary to decimal.
you can use strings or lists to contain binary code.
this is limited to 31bits ( sl limitation )
CODE

// limited to 31bits
// ================================
// BIN TO DEC ( list )
// ================================
integer binList2Dec(list binArray)
{
integer dec = 0;
integer factor = 1;
integer i;
integer size = llGetListLength(binArray);
for ( i=(size-1);i>=0;--i )
{
dec += llList2Integer(binArray,i)*factor;
factor *= 2;
}
return dec;
}
// ================================
// BIN TO DEC ( string )
// ================================
integer binString2Dec(string binString)
{
integer dec = 0;
integer factor = 1;
integer i;
integer size = llStringLength(binString);
for ( i=(size-1);i>=0;--i )
{
dec += ((integer)llGetSubString(binString,i,i))*factor;
factor *= 2;
}
return dec;
}
// ================================
// DEC TO BIN ( list )
// ================================
list dec2BinList(integer dec)
{
integer i;
integer j;
string result="";
while (dec != 1)
{
j = dec % 2;
i = (dec - j) / 2;
dec = i;
result = (string)j + "," + result;
}
result = llDeleteSubString(("1" + "," + result),-1,-1);
list resulList = stringList2IntegerList(llCSV2List(result));
return resulList;
}
list stringList2IntegerList( list input )
{
integer inputSize = llGetListLength(input);
integer i;
for ( i=0;i<(inputSize-1);++i )
{
input = llListReplaceList(input, [llList2Integer(input,i)],i,i);
}
return input;
}
// ================================
// DEC TO BIN ( string )
// ================================
string dec2BinString(integer dec)
{
integer i;
integer j;
string result="";
while (dec != 1)
{
j = dec % 2;
i = (dec - j) / 2;
dec = i;
result = (string)j + result;
}
result = "1" + result;
return result;
}
// ================================
// TESTS
// ================================
default
{
state_entry()
{
llOwnerSay((string)binList2Dec([1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]));
llOwnerSay((string)binString2Dec("1111111111111111111111111111111"));// 31 bits
llOwnerSay((string)binList2Dec([0,1])); //1
llOwnerSay((string)binList2Dec([0,0,0,0,1,0])); //2
llOwnerSay((string)binList2Dec([0,0,0,0,0,0,0,0,0,0,0,0,1,1])); //3
llOwnerSay((string)binList2Dec([0,0,0,0,0,0,0,0,0,1,0,0])); //4
llOwnerSay((string)binList2Dec([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1])); //5
llOwnerSay((string)binList2Dec([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0])); //6
llOwnerSay((string)binList2Dec([1,1,1])); //7
llOwnerSay((string)binList2Dec([1,0,0,0])); //8
llOwnerSay((string)binList2Dec([1,0,0,1])); //9
llOwnerSay((string)binList2Dec([1,0,1,0])); //10
llOwnerSay((string)binList2Dec([1,0,1,1])); //11
llOwnerSay((string)binString2Dec("01")); //1
llOwnerSay((string)binString2Dec("10")); //2
llOwnerSay((string)binString2Dec("0000000011")); //3
llOwnerSay((string)binString2Dec("000000000101")); //4
llOwnerSay((string)binString2Dec("0000000101")); //5
llOwnerSay((string)binString2Dec("00000000000000000110")); //6
llOwnerSay((string)binString2Dec("00000000111")); //7
llOwnerSay((string)binString2Dec("0000000001000")); //8
llOwnerSay((string)binString2Dec("000000000000000001001")); //9
llOwnerSay((string)binString2Dec("0000000001010")); //10
llOwnerSay(dec2BinString(256));
llOwnerSay(dec2BinString(1));
llOwnerSay(dec2BinString(2));
llOwnerSay(dec2BinString(3));
llOwnerSay(dec2BinString(4));
llOwnerSay(llList2CSV(dec2BinList(256)));
llOwnerSay(llList2CSV(dec2BinList(1)));
llOwnerSay(llList2CSV(dec2BinList(2)));
llOwnerSay(llList2CSV(dec2BinList(3)));
llOwnerSay(llList2CSV(dec2BinList(4)));
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Library bump
06-26-2008 12:18
:)
_____________________
i've got nothing. ;)
Coder Kas
Registered User
Join date: 1 Jun 2008
Posts: 2
06-29-2008 20:22
Useful, thankies :)