User:Abstract Alchemi: Difference between revisions
Jump to navigation
Jump to search
I wanted to post my script here, and expand on it later, and possiably post it in the LSL Portal when I get it more developed, but for now it goes here! |
Fixed, an error! |
||
| Line 6: | Line 6: | ||
string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVW"; | string code = "0123456789ABCDEFGHIJKLMNOPQRSTUVW"; | ||
string base23; | string base23; | ||
if(int < 23) return llGetSubString(code,int | if(int < 23) return llGetSubString(code,int,int); | ||
while(int > 23){ | while(int > 23){ | ||
integer mod = int%23; | integer mod = int%23; | ||
Revision as of 21:30, 21 January 2010
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>