Author Topic: Base Conversions  (Read 1955 times)

0 Members and 1 Guest are viewing this topic.

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Base Conversions
« on: July 23, 2008, 03:23:59 pm »
Script Name: Base Conversions... idk (I suck at names)
Script Description:
This script contains 2 functions:
function toString(bin: integer; radix: integer): String
toString 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): Integer
parseInt 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.html
was a big help for toString.
Core Version: 2.2 or whatever.
Code:
Heres the whole thing, its not too long.
Code: [Select]
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.
« Last Edit: July 23, 2008, 04:34:00 pm by iDante »

Offline As de Espada

  • Soldat Beta Team
  • Veteran
  • ******
  • Posts: 1493
  • Mapper
    • My maps
Re: Base Conversions
« Reply #1 on: July 23, 2008, 03:48:13 pm »
you must have something with hexadecimal xD
All my maps | my latest map: SoldatX Racing Mappack
me making a map on youtube: ctf_FastMade

Offline Iq Unlimited

  • Flagrunner
  • ****
  • Posts: 864
  • mr. foobar2000
Re: Base Conversions
« Reply #2 on: July 23, 2008, 04:27:18 pm »
Yes, you must.

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Base Conversions
« Reply #3 on: July 23, 2008, 04:31:00 pm »
you must have something with hexadecimal xD
Yes, you must.
... what does that mean?

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Base Conversions
« Reply #4 on: July 23, 2008, 05:35:33 pm »
Nice idea.
If I were to make this, I would have done something like..

Code: [Select]
type
  tNumber = record
    Base: byte;
    Value, CharSet: string;
  end;
procedure BaseConvert(Input: tNumber; var Output: tNumber);
where as Output's given charset and base are used to modify the value of Output's Value

or maybe..

Code: [Select]
const
  DefCharSet = '0123456789ABDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
function BaseConvert(InValue: string; InBase: byte; InCharSet: string; OutBase: byte; OutCharSet: string): string;
then simply do StrtoInt(BaseConvert('FF', 16, DefCharSet, 10, DefCharSet)) to change to string in a given base into base 10

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: Base Conversions
« Reply #5 on: July 23, 2008, 10:31:53 pm »
I guess that kinda makes sense (a little confusing but I think I get it).
I was really just trying to replicate parseInt and toString from java because I was bored.

I'm prolly gonna add in a bit to allow signed stuff, but IDK exactly how for now. *consults godly google*
Edit: eeh thats prolly not gonna happen actually.
« Last Edit: July 23, 2008, 11:14:47 pm by iDante »