# How to convert from integer to binary

And  no mater how many big integer number is ( but tollways less than 255 (8 bits)) the binary number has to have eight characters.

example:

integer 1 = binar 00000001

How can i do that?

###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
function TForm1.IntToBinary(Int: Integer;Digits: Byte): string;
const CONVERT:array[0..1] of Char = '01';
var cnt: Integer;
begin
SetLength(Result, Digits);
for cnt := Digits downto 1 do begin
Result[cnt] := CONVERT[Int mod 2];
Int := Int shr 1;
end;
end;

ziolko.
0
Commented:
Int is integer to be converted to binary
Digits is length of output string
so if you want convert 12 to binary with 8 digits use: IntTOBinary(12, 8)

ziolko.
0
Commented:
oh i see what he's asking it looks like he wants to convert integer to binary but he's not....

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{\$R *.DFM}

function BitsToByte(Str :String ):Byte;
var
b: Byte;
N: Integer;
I: Byte;
begin
I:=128;
B:=B xor b;
For N:=1 to Length(Str) do
Begin
If Str[N]='1' Then
B:=B Or I;
I:=I Shr 1;
End;
Result:=B;
end;

procedure TForm1.Button1Click(Sender: TObject);
Var
N: Integer;
begin
For N:=1 to BitsToByte('00000100') do  //1 to 4
Memo1.Lines.Add(IntToStr(N)); //displays 4 lines in memo1
end;

end.
0
Commented:
>>oh i see what he's asking it looks like he wants to convert integer to binary but he's not....

I don't think so, just look at Q's title

ziolko.
0
Commented:
function IntToBin1(Value: Longint; Digits: Integer): string;
var
i: Integer;
begin
Result := '';
for i := Digits-1 downto 0 do
if Value and (1 shl i) <> 0 then
Result := Result + '1'
else
Result := Result + '0';
end;
0
Commented:
>>I don't think so, just look at Q's title

I don't think so, look at the example.

example:

integer 1 = binar 00000001

How can i do that?
0
Commented:
>>integer 1 = binar 00000001

he wants to convert integer 1 into string 0000001

ziolko.
0
Commented:

function TForm1.IntToBinary(Int: Integer;Digits: Byte): string;
const CONVERT:array[0..1] of Char = '01';
var cnt: Integer;
begin
SetLength(Result, Digits);
for cnt := Digits downto 1 do begin
Result[cnt] := CONVERT[Abs(Int) mod 2];
Int := Int shr 1;
end;
end;

ziolko.
0

Experts Exchange Solution brought to you by