# Binary and Decimal Conversion Functions

## Recommended Posts

I wrote some functions to change a decimal number into a binary number then also wrote a function to turn a binary string into a decimal number. These functions im sure will help someone understand binary a little better.

[scar]

{~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

function IntToBin(I: Integer): String;

Description: Turns decimal number into a string of binary digits.

Date Created: September, 5th 2012. By: ShadowRecon.

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~}

function IntToBin(I: Integer): String;

var

Rem: Extended;

Bin_Number: String;

Done: Boolean;

begin

Done := False;

Result := '';

Bin_Number := '';

{

In this repeat loop im looping throguh and only looking at the remainder

using the Mod function if the remainder is not 0 then a 1 is added to the

binary number.

}

repeat

Rem := I mod 2;

if Rem = 0 then Bin_Number := Bin_Number + '0'

else Bin_Number := Bin_Number + '1';

I := I div 2;

if I = 0 then

Done := true;

until Done;

{

The For loop below, re-organizes the 1's and 0's from last to first.

}

for I := Length(Bin_Number) downto 1 do

Result := Result + Bin_Number;

end;

{~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~

function BinToInt(Bin: String): Integer;

Description: Turns a string binary digits into a decimal number.

Date Created: September, 5th 2012. By: ShadowRecon.

~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~}

function BinToInt(Bin: String): Integer;

var

I, Counter: Integer;

begin

Counter := 0;

for I := Length(Bin) downto 1 do

begin

if Char(Bin) = '1' then

Result := Result + Round(Pow(2, Counter));

inc(Counter);

end;

end;

begin

Writeln(IntToBin(96));

Writeln(BinToInt('1100000'));

end.

[/scar]

##### Share on other sites

Nice But you can shorten these a lot if you just bitshift. This should also be quite a bit faster, bitwise operations are amongst the fastest you can perform generally.

[scar]function IntToBin(Value: Integer): string;

begin

Result := '';

while Value > 0 do

begin

if Boolean(Value and 1) then

Result := '1' + Result

else

Result := '0' + Result;

Value := Value shr 1;

end;

end;

function BinToInt(Value: string): Integer;

var

I, L: Integer;

begin

Result := 0;

L := Length(Value);

for I := 1 to L do

begin

Result := Result shl 1;

if Value = '1' then

Result := Result or 1;

end;

end;[/scar]

##### Share on other sites

Nice But you can shorten these a lot if you just bitshift. This should also be quite a bit faster, bitwise operations are amongst the fastest you can perform generally.

Very nice, i didnt think about bit shifting.. those are some of the most basic functions used by the processor. I am taking a digital circuits class and they have been going over binary and hex and octal numbering systems so i figured id try my hand at writing a function for ea of them so i can better under how to manipulate the numbers between the bases.

##### Share on other sites

Very nice, i didnt think about bit shifting.. those are some of the most basic functions used by the processor. I am taking a digital circuits class and they have been going over binary and hex and octal numbering systems so i figured id try my hand at writing a function for ea of them so i can better under how to manipulate the numbers between the bases.

Your going to be so fluent in bit operations lol! You already taught me it once, so here you are again with it, I think I forget some of it. You can teach me again if you ever want to lol.

Remembering when we went over all this on skype for some time

##### Share on other sites

My main point is that as you say, the bitwise operations are oen of the most basic operations a CPU can perform, which makes them blazing fast, where as the division, power and rounding operations you use are very expensive. Also, whenever possible, don't use decimal numbers, these are also a lot more expensive to use than integers. but I guess you probably know all of that already if you're taking a circuits class Have you learned how to convert floating point numbers into binary yet?

##### Share on other sites

My main point is that as you say, the bitwise operations are oen of the most basic operations a CPU can perform, which makes them blazing fast, where as the division, power and rounding operations you use are very expensive. Also, whenever possible, don't use decimal numbers, these are also a lot more expensive to use than integers. but I guess you probably know all of that already if you're taking a circuits class Have you learned how to convert floating point numbers into binary yet?

Yeah i know all the rounding and math defeats the purpose of bitwise being fast but i just made it so it would work properly but you on the other hand are an expert at making things efficient.

And yes i learned how to convert floating point, to binary, its almost the same except u use a negative power for each position.

##### Share on other sites

オンライン カジノは、プレイヤーが自宅にいながらにしてポーカー、ルーレット、ブラックジャック、スロットなどのギャンブル ゲームを楽しむ機会を提供する仮想プラットフォームです。 オンラインカジノは、アクセスのしやすさ、ゲームの種類の多さ、そして大金を獲得する機会があるため、年々人気が高まっています。

オンラインカジノの主な利点は、利便性とアクセスしやすさです。 プレイヤーは、通常のカジノの営業時間に制限されず、いつでもゲームを楽しむことができます。 必要なのは、インターネットにアクセスできるデバイスと、カジノのウェブサイトにアクセスできることだけです。 これにより、プレイヤーは従来のカジノによくありがちなストレスや緊張を感じることなく、快適な環境でプレイすることができます。

オンラインカジノのもう1つの利点は、ゲームの選択肢が豊富なことです。 ユーザーは、それぞれ独自のルールと勝利の機会を提供する何百もの異なるゲームから選択できます。 技術革新のおかげで、オンライン ゲームのグラフィックとサウンドは高品質になり、プレイヤーは興奮と情熱の雰囲気に浸ることができます。

さまざまなゲームに加えて、オンライン カジノはプレーヤーにさまざまなボーナスやプロモーションも提供します。 これらは、スロットのフリースピン、プレイのための追加のお金、または貴重な賞品が得られる特別なトーナメントなどです。 このようなボーナスにより、勝利の可能性が高まり、ゲームがさらに楽しくなります。

もちろん、オンラインカジノでのプレイにはリスクがあります。 ギャンブルには依存性がある可能性があるため、自分の感情を監視し、支出をコントロールすることが重要であることを覚えておくことが重要です。 カジノはまた、責任あるゲーミングをサポートし、自己排除や賭け金制限の機会を提供します オンラインカジノ

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×