User:Abstract Alchemi

From Second Life Wiki
Revision as of 21:30, 21 January 2010 by Abstract Alchemi (talk | contribs) (Fixed, an error!)
Jump to navigation Jump to search

Scripts

Out of some interest in Bases, I wanted to make something that could convert from Base-10 to any other bases. I started off reading documentation on how to do so, and I decided i'd try just to convert from Base-23 to Base-10. Here are my results!

Base-23 and Base-10

<lsl> string IntegerToBase23(integer int){

   string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVW";
   string base23;
   if(int < 23) return llGetSubString(code,int,int);
   while(int > 23){
       integer mod = int%23;
       int = int/23;
       base23 += llGetSubString(code,mod,mod);
   }
   if(int != 0) base23 += llGetSubString(code,int,int); string str; integer i;
   for(i = llStringLength(base23)-1; i >= 0; i--) str += llGetSubString(base23,i,i);
   return str;

} integer Base23ToInteger(string src){

   integer base23; integer i;
   string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVW";
   for(i = 0; i < llStringLength(src);i++){
       if(llSubStringIndex(code,llGetSubString(src,i,i)) == -1) return -1;
       base23 += llSubStringIndex(code,llGetSubString(src,i,i))*(integer)llPow(23,llAbs(i-llStringLength(src)+1));
   } 
   return base23;

}

default{

   state_entry(){
       integer num = 10;
       llSay(0,(string)IntegerToBase23(num)+" == "+(string)Base23ToInteger(IntegerToBase23(num)));
   }

} </lsl>