User:Abstract Alchemi

From Second Life Wiki
Jump to: navigation, search

Scripts

Out of some interest in Bases, I wanted to make something that could convert from Base-10 to any other base. Here are my results!

Base-N (1 < N < 67)

<lsl> string integertobase(integer base, integer int){

   int = llAbs(int); if(base > 66||base < 1||base == 10) return "-1";
   string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   string baseInteger;
   while(int > base){
       integer mod = int%base;
       int = (integer)llList2String(llParseString2List((string)(int/base),["."],[]),0);
       baseInteger += llGetSubString(code,mod,mod);
   }
   if(int != 0) baseInteger += llGetSubString(code,int,int); string str; integer i;
   for(i = llStringLength(baseInteger)-1; i >= 0; i--) str += llGetSubString(baseInteger,i,i);
   return str;

}

integer basetointeger(integer base, string src){

   if(base > 66||base < 1||base == 10) return -1;
   integer baseInteger; integer i;
   string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
   for(i = 0; i < llStringLength(src);i++){
       if(llSubStringIndex(code,llGetSubString(src,i,i)) == -1) return -1;
       baseInteger += llSubStringIndex(code,llGetSubString(src,i,i))*(integer)llPow(base,llAbs(i-llStringLength(src)+1));
   } 
   return baseInteger;

} </lsl>