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?

pr2501Asked:
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.

ziolkoCommented:
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
ziolkoCommented:
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
tobjectpascalCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

ziolkoCommented:
>>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
diniludCommented:
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
tobjectpascalCommented:
>>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
ziolkoCommented:
>>integer 1 = binar 00000001

he wants to convert integer 1 into string 0000001

ziolko.
0
ziolkoCommented:
ahhh forgot about negative numbers:

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

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pr2501Author Commented:


  it's ok (i work only with positive numbers).
 thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.