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?
 
ziolkoConnect With a Mentor Commented:
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
 
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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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
 
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
 
pr2501Author Commented:


  it's ok (i work only with positive numbers).
 thank you
0
All Courses

From novice to tech pro — start learning today.