ID:195046   Aug 31 2008, 6:16 pm ```/* Title: Base Conversion Credit to: William George Horner (Horner's Algorithm) Contributed by: CaptFalcon33035 Programmed by: CaptFalcon33035This is a utility that makes base conversion easy. It is different from what is currently on the Snippet Database in the aspects of speed and flexibility. Whatis currently listed on the Snippet Database (by Popisfizzy) is much faster thanwhat we've got here, but is limited to 16 bits whereas this snippet is limitedto the amount of data floating point numbers can record accurately.*/#define BASE_BINARY 2#define BASE_QUATERNARY 4#define BASE_OCTAL 8#define BASE_DECIMAL 10#define BASE_HEXADECIMAL 16#define BASE_LIMIT 16var/base_chars[]=\ list("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F") proc/base_getChar(var/char) return (base_chars.Find(char)-1)proc/base_pullPos(var/pos) return (base_chars[pos+1]) proc/string2list(var/string /*, var/edit */) if(!string || !istext(string)) return string var/L[lentext(string)] //if(edit) for(var/i=1 to L.len) L[i]=call(edit)(copytext(string,i,i+1)) //else for(var/i=1 to L.len) L[i]=copytext(string,i,i+1) for(var/i=1 to L.len) L[i] = copytext(string, i, i+1) return Lproc/bin2dec(var/number) return base2dec(number, BASE_BINARY)proc/quat2dec(var/number) return base2dec(number, BASE_QUATERNARY)proc/oct2dec(var/number) return base2dec(number, BASE_OCTAL)proc/hex2dec(var/number) return base2dec(number, BASE_HEXADECIMAL)proc/base2dec(var/number, var/base=BASE_BINARY) if(!number || !istext(number)) return number ASSERT(base <= BASE_LIMIT && base >= BASE_BINARY) var/characters[] = string2list(uppertext(number)) var/result = 0 if(characters.len > 1) result = (base_getChar(characters[1]) * base) for(var/i=2 to characters.len-1) result = ((result + base_getChar(characters[i])) * base) return result + base_getChar(characters[characters.len])proc/dec2bin(var/number) return dec2base(number, BASE_BINARY)proc/dec2quat(var/number) return dec2base(number, BASE_QUATERNARY)proc/dec2oct(var/number) return dec2base(number, BASE_OCTAL)proc/dec2hex(var/number) return dec2base(number, BASE_HEXADECIMAL)proc/dec2base(var/number, var/base=BASE_BINARY) if(!number || !isnum(number)) return number ASSERT(base <= BASE_LIMIT && base >= BASE_BINARY) number = round(number) var/result = "" while(number) result = base_pullPos(number % base) + result number = round(number / base) return result ```