SRL Archive - SRL Archives

Binary to Decimal and Decimal to Binary functions!


Ron

2007-02-09 22:39:21

Binary to Decimal and Decimal to Binary functions!


Binary to Decimal

// Converts Binary to Decimal.
// Made by Ron. :)
function BinToDec(Str : string) : integer;
var
i : integer;
begin
if(Length(Trim(Str)) = 0)then
Exit;
for i := 1 to Length(Str) do
Result := Result*2 + StrToInt(Str[i]);
end;

Decimal to Binary

// Converts Decimal to Binary.
// Made by Ron. :)
function DecToBin(Number : integer) : string;
begin
while Number > 1 do
begin
Result := IntToStr(Number mod 2)+Result;
Number := Number/2;
end;
Result := '1' + Result;
end;

~Ron

Ejjman

2007-02-10 16:52:25

Thats cool, except could you give a case in which you would use it? Thanks

-Ejj

Ron

2007-02-10 18:32:15

Here's one case. When you want to convert binary to decimal or vice versa. :)

Seriously though. Hmm. Well for fun I made an encrypt/decrypt procedure lol. Here they are. They use the above functions.


// Encrypts the string by converting each letter to binary.
// Made by Ron :)
function Encrypt(Str : string) : string;
var
i : integer;
begin
if(Length(Trim(Str)) = 0)then
Exit;
for i := 1 to Length(Str) do
Result := Result + DecToBin(Ord(Str[i])) + '-';
end;

// Decrypts each binary letter and creates the original string.
// Made by Ron :)
function Decrypt(Str : string) : string;
begin
while pos('-',Str) > 0 do
begin
Result := Result + chr(BinToDec(copy(Str,1,pos('-',Str)-1)));
delete(Str,1,pos('-',Str));
end;
end;

~Ron :)

Ejjman

2007-02-10 21:08:16

Hm, I can't get it to work :\

Successfully compiled
Successfully exctecuted (sp?)

So ya. :\

Ron

2007-02-10 22:03:21

Run this. And change the Text to whatever you want and it will encrypt and decrypt it.


program TestBinaryAndEncryptFunctionsByRon;

const
Text = 'lolwow';

var
i : integer;

function DecToBin(Number : integer) : string;
begin
while Number > 1 do
begin
Result := IntToStr(Number mod 2)+Result;
Number := Number/2;
end;
Result := '1' + Result;
end;

function BinToDec(Str : string) : integer;
var
i : integer;
begin
if(Length(Trim(Str)) = 0)then
Exit;
for i := 1 to Length(Str) do
Result := Result*2 + StrToInt(Str[i]);
end;

function Encrypt(Str : string) : string;
var
i : integer;
begin
if(Length(Trim(Str)) = 0)then
Exit;
for i := 1 to Length(Str) do
Result := Result + DecToBin(Ord(Str[i])) + '-';
end;

function Decrypt(Str : string) : string;
begin
while pos('-',Str) > 0 do
begin
Result := Result + chr(BinToDec(copy(Str,1,pos('-',Str)-1)));
delete(Str,1,pos('-',Str));
end;
end;

begin
ClearDebug;
WriteLn('Encrypted: '+Encrypt(Text));
WriteLn('Decrypted: '+Decrypt(Encrypt(Text)));
end.

Ejjman

2007-02-11 13:27:10

Wow, thats pretty sweet, except I don't get where'd u'd use it in a scripting situation. but ya my name is…

1100101-1100001-1110010-1101100-

lol

Dark_Sniper

2007-02-11 17:45:51

ummmmmm

my name is:

1010100-1101000-1100101-1011111-1000100-1100001-1110010-1101011-1011111-1010011-1101110-1101001-1110000-1100101-1110010-

my name is so big…..

Ron

2007-02-11 20:01:45

Haha glad you guys like it now.

So ejjiman's name is earl. :)

The_Dark_Sniper, nice.

Ejjman

2007-02-11 22:21:46

Lol thats my name, dont u forget it :P How do you convert back? Oh, I think I could figure it out :P

EDIT: Oh easy lol

Dark_Sniper

2007-02-12 11:21:38

The_dark_sniper's identity will be kept secret for yet another day…

t3hl33tn00b

2007-02-12 20:00:06

1110100-110011-1101000-1101100-110011-110011-1110100-1101110-110000-110000-1100010-100000-1101111-1110111-1101110-1110011-100000-1111001-1101111-1110101-
wow i have a long name :P

Cheesehunk

2007-02-12 20:30:31

Encrypted: Chuck Norris
Decrypted: Chuck Norris
Looks like my name is un-cryptable

Ejjman

2007-02-12 23:13:21

From: Cheesehunk
Encrypted: Chuck Norris
Decrypted: Chuck Norris
Looks like my name is un-cryptable


Pwnd IRL >.<

Boreas

2007-02-12 23:21:39

From: Cheesehunk
Encrypted: Chuck Norris
Decrypted: Chuck Norris
Looks like my name is un-cryptable

That made my day…

Ron

2007-02-13 05:11:05

Liar!

1000011-1101000-1110101-1100011-1101011-100000-1001110-1101111-1110010-1110010-1101001-1110011-100000-1101010-1101111-1101011-1100101-1110011-100000-1110011-1110101-1100011-1101011-100001-100000-1010011-1101111-1110010-1110010-1111001-101100-100000-1100010-1110101-1110100-100000-1110100-1101000-1100101-1111001-100000-1101010-1110101-1110011-1110100-100000-1101110-1100101-1110110-1100101-1110010-100000-1101101-1100001-1101011-1100101-100000-1100001-1101110-1111001-100000-1110011-1100101-1101110-1110011-1100101-100001-