Log in

View Full Version : binary



Feroc1ty
02-02-2011, 08:58 PM
Does anyone have a simba function to convert int to binary and binary to int?

Zyt3x
02-02-2011, 09:52 PM
You could always make your own :)
function HexToStr(Hex : String) : String;
var
I, X, L, H : Integer;
sArr, hArr : TStringArray;
begin
Result := '';
sArr := [' ', '!', '"', '#', '$', '%', '&', '''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'];
hArr := ['20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2A', '2B', '2C', '2D', '2E', '2F', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3A', '3B', '3C', '3D', '3E', '3F', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4A', '4B', '4C', '4D', '4E', '4F', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5A', '5B', '5C', '5D', '5E', '5F', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6A', '6B', '6C', '6D', '6E', '6F', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7A', '7B', '7C', '7D', '7E'];
L := Length(Hex);
H := High(hArr);
for I := 0 to L do
if I mod 2 <> 0 then
for X := 0 to H do
if Hex[I] + Hex[I+1] = hArr[X] then
Result := Result + sArr[X];
end;

function StrToHex(Str : String) : String;
var
I, X, L, ArrH : Integer;
sArr, hArr : array of String;
begin
Result := '';
sArr := [' ', '!', '"', '#', '$', '%', '&', '''', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'];
hArr := ['20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2A', '2B', '2C', '2D', '2E', '2F', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3A', '3B', '3C', '3D', '3E', '3F', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4A', '4B', '4C', '4D', '4E', '4F', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5A', '5B', '5C', '5D', '5E', '5F', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6A', '6B', '6C', '6D', '6E', '6F', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7A', '7B', '7C', '7D', '7E'];
L := Length(Str);
ArrH := High(sArr);
for I := 1 to L do
for X := 0 to ArrH do
if Str[I] = sArr[X] then
Result := Result + hArr[X];
end;

function BinaryToHex(Bin : String) : String;
var
I, L : Integer;
begin
Result := '';
L := Length(Bin);
for I := 1 to L do
if I mod 4 = 1 then
case Bin[I] + Bin[I+1] + Bin[I+2] + Bin[I+3] of
'0000': Result := Result + '0';
'0001': Result := Result + '1';
'0010': Result := Result + '2';
'0011': Result := Result + '3';
'0100': Result := Result + '4';
'0101': Result := Result + '5';
'0110': Result := Result + '6';
'0111': Result := Result + '7';
'1000': Result := Result + '8';
'1001': Result := Result + '9';
'1010': Result := Result + 'A';
'1011': Result := Result + 'B';
'1100': Result := Result + 'C';
'1101': Result := Result + 'D';
'1110': Result := Result + 'E';
'1111': Result := Result + 'F';
end;
end;

function HexToBinary(Hex : String) : String;
var
I, L : Integer;
begin
Result := '';
L := Length(Hex);
for I := 1 to L do
case Hex[I] of
'0': Result := Result + '0000';
'1': Result := Result + '0001';
'2': Result := Result + '0010';
'3': Result := Result + '0011';
'4': Result := Result + '0100';
'5': Result := Result + '0101';
'6': Result := Result + '0110';
'7': Result := Result + '0111';
'8': Result := Result + '1000';
'9': Result := Result + '1001';
'A': Result := Result + '1010';
'B': Result := Result + '1011';
'C': Result := Result + '1100';
'D': Result := Result + '1101';
'E': Result := Result + '1110';
'F': Result := Result + '1111';
end;
end;

function StrToBinary(Str : String) : String;
begin
Result := HexToBinary(StrToHex(Str));
end;

function BinaryToStr(Bin : String) : String;
begin
Result := HexToStr(BinaryToHex(Bin));
end;

function IntToBinary(Int : Integer) : String;
begin
Result := StrToBinary(IntToStr(Int));
end;

function BinaryToInt(Bin : String) : Integer;
begin
Result := StrToIntDef(BinaryToStr(Bin), -1);;
end;

Frement
02-02-2011, 10:01 PM
Don't know of any, where do you need it? I could probably make one for you if no one else has one.

EDIT: @Zyt3x: I'm sure there are more easier ways then that?

**BANNED The Man
02-02-2011, 10:08 PM
I don't know what you are using this for...
Can you convert to Extended?

Zyt3x
02-02-2011, 10:29 PM
@Zyt3x: I'm sure there are more easier ways then that?Me too :)

**BANNED The Man
02-02-2011, 10:52 PM
IntToBinary
function IntToBinary(value:LongInt; digits:integer):string;
begin
result := StringOfChar ('0', digits) ;
while value > 0 do begin
if (value and 1) = 1 then
result [digits] := '1';
dec (digits) ;
value := value shr 1;
Writeln(value);
end;
end;
and
BinaryToInt
function BinaryToInt(Value: String): LongInt;
var i: Integer;
begin
Result:=0;
while Copy(Value,1,1)='0' do
Value:=Copy(Value,2,Length(Value)-1) ;
for i:=Length(Value) downto 1 do
if Copy(Value,i,1)='1' then
Result:=Result+(1 shl (Length(Value)-i)) ;
end;

They, SHOULD work. :)

-Boom

Hobbit
02-02-2011, 10:55 PM
Learn to do the math, it's not hard. Itd probably take you longer to put into a script than just doing the math to convert it yourself.

Feroc1ty
02-03-2011, 06:19 AM
I wasn't asking for people to make it, I was simply seeing if anyone had it already made, sorry if you guys misunderstood me; and for anyone wondering... it's for an array of locations which change depending upon 4 boolean setting values.

Hobbit
02-03-2011, 06:26 AM
That sounds very interesting!

Explain a little more? :D or is it a secret project? :eek:

Sex
02-03-2011, 06:39 AM
So 16 locations?

Hobbit
02-03-2011, 06:41 AM
So 16 locations?

How do you get that? There's not a 4 bit cap on binary.

Sex
02-03-2011, 06:50 AM
Wait, you right. Aha, I'm hella dumb, haven't worked with none of this in a while :/.

Feroc1ty
02-03-2011, 10:19 AM
No, he's right, 16 locations. Binary number holding 4 boolean values.

Goes from "0000" to "1111", so I'll have location for each possible combination of the boolean values.

edit: here's what im making hobbit didnt propertly read ur post

var
// Spell toggles (0:combat,1:teleport,2:misc,3:skill,4:defensive)
fMagic_toggles:array [0..4] of boolean;
fMagic_toggle_loc:array [0..4] of TBox;

// Current spell category toggles
fMagic_mode:integer;

type fMagic_location = record
loc:array [0..15]of TBox;
visible:array [0..15] of boolean;
default_mode:integer;
end;

procedure fMagic_changemode(mode:integer);
var i:integer;
n,o:string;
begin
o := IntToBinary(fMagic_mode,4);
fMagic_mode := mode;
n := IntToBinary(mode,4);
for i := 1 to 4 do
begin
if not(n[i] = o[i]) then
begin
{fMagic_toggles[i-1] := IntToBool(StrToInt(n[i]));
ClickBox(fMagic_toggle_loc[i-1], true);
fDebug('fMagic','Changing fMagic_toggles['+tostr(i-1)+'] to '+tostr(inttobool(n[i]))+'.');}
end;
end;
end;

function fMagic_click(slot:fMagic_location):boolean;
begin
//if not GetGameTab = tab_magic then GameTab(tab_magic);
if slot.visible[fMagic_mode] then
begin
fDebug('fMagic','Spell slot is visible, clicking.');
ClickBox(slot.loc[fMagic_mode],true);
end else
begin
fDebug('fMagic','Spell slot is not visible, changing spell toggles.');
fMagic_changemode(slot.default_mode);
ClickBox(slot.loc[fMagic_mode],true);
end;
end;

type fMagic_spell = record
loc:fMagic_location;
typ:integer;
end;

var

fMagic_Teleboxes:array [0..12] of TBox;
fMagic_falador,fMagic_telegrab:fmagic_spell;


function fTeleport(city:string):boolean;
var c:integer;
a:array [0..12] of boolean;
begin
case city of
'home': c:= 0;
'armies': c:= 1;
'varrock': c:= 2;
'lumbridge': c:= 3;
'falador': c:= 4;
'house': c:= 5;
'camelot': c:= 6;
'watchtower': c:= 7;
'trollheim': c:= 8;
'ape': c:= 9;
'otherlumbridge': c:=10;
'otherfalador': c:=11;
'othercamelot': c:=12;
end;

a[4] := true;

if not a[c] then
begin
fDebug('fMagic','This teleport is not yet supported!');
break;
end;

case c of
4: fMagic_click(fMagic_falador);
end;

result := MeInBox(fMagic_teleboxes[c]);

end;

procedure fMagic();
begin
fMagic_falador.typ := 1;
fMagic_falador.loc.default_mode := 6;
fMagic_falador.loc.loc[6] := TBox(564,245,580,261);
fMagic_falador.loc.visible[6] := true;

fMagic_telegrab.typ := 2;
fMagic_telegrab.loc.default_mode := 6;
fMagic_telegrab.loc.loc[6] := TBox(707,222,725,235);
fMagic_telegrab.loc.visible[6] := true;
end;

barely started it, but im gonna create a magic include for myself (and anyone that wants it) to cast spells no matter the spell type toggles, it will strictly use locations rather than stuff such as DTM's, images, or colors