Difference between revisions of "User:Abstract Alchemi"

From Second Life Wiki
Jump to navigation Jump to search
(Fixed, an error!)
m (Another minor issue fixed.)
Line 1: Line 1:
==Scripts==
==Scripts==
Out of some interest in [http://en.wikipedia.org/wiki/Base_(mathematics) Bases], I wanted to make something that could convert from [http://en.wikipedia.org/wiki/Decimal 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 [http://en.wikipedia.org/wiki/Decimal Base-10]. Here are my results!
Out of some interest in [http://en.wikipedia.org/wiki/Base_(mathematics) Bases], I wanted to make something that could convert from [http://en.wikipedia.org/wiki/Decimal Base-10] to any other base. I started off reading documentation on how to do so, and I decided i'd try just to convert from Base-23 to [http://en.wikipedia.org/wiki/Decimal Base-10]. Here are my results!
==Base-23 and [http://en.wikipedia.org/wiki/Decimal Base-10]==
==Base-23 and [http://en.wikipedia.org/wiki/Decimal Base-10]==
<lsl>
<lsl>

Revision as of 21:41, 21 January 2010

Scripts

Out of some interest in Bases, I wanted to make something that could convert from Base-10 to any other base. 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>