Script Name: Base Conversions... idk (I suck at names)
Script Description:This script contains 2 functions:
function toString(bin: integer; radix: integer): StringtoString creates a string out of the int. It is basically inttostr, HOWEVER, it lets you supply a radix to convert the int to another base with. For instance:
toString(7,2) returns '111', the binary (base 2) representation of 7.
toString(28,16) returns '1c', the hex version of 28.
function parseInt(bin: String; radix: Integer): IntegerparseInt takes a String integer in base
radix and turns it into a base 10 integer.
For example:
parseInt('111',2) returns 7, and
parseInt('1c',16) returns 28.
Original Author(s): iDante
Edit: forgot to stick this link in here:
http://www.cs.uaf.edu/~cs301/notes/Chapter3/node7.htmlwas a big help for toString.
Core Version: 2.2 or whatever.
Code:Heres the whole thing, its not too long.
function parseInt(bin: String; radix: Integer): Integer;
var
i,p,q,s,r: Integer;
begin
for i:=1 to length(bin) do begin
p := length(bin) - i;
r := 1;
if(ord(bin[i])-ord('0') >= 0) AND (ord(bin[i])-ord('0') <= 9) then begin
for q := 1 to p do r := r * radix;
Result := Result + (ord(bin[i]) - ord('0'))*r;
end else if(ord(bin[i])-ord('a') >= 0) AND (ord(bin[i])-ord('a') <= 27) then begin
for q := 1 to p do r := r * radix;
Result := Result + (ord(bin[i]) - ord('a') + 10)*r;
end else if(ord(bin[i])-ord('A') >= 0) AND (ord(bin[i])-ord('A') <= 27) then begin
for q := 1 to p do r := r * radix;
Result := Result + (ord(bin[i]) - ord('A') + 10)*r;
end else begin
Result := 0;
end;
end;
end;
function toString(bin: integer; radix: integer): String;
var
all: String;
q,p: integer;
begin
all := '0123456789abcdefghijklmnopqrstuvwxyz';
Result := '';
p := bin;
repeat
q := p mod radix;
p := p / radix;
Result := all[q+1]+Result;
until p = 0;
end;
Like always, I like code fixes (it works but I'm sure there are bits that can be improved) and elegantizers, etc. Suggestions welcome.