Recent

Author Topic: Decimal to binary converter  (Read 18201 times)

paradox2397

  • New member
  • *
  • Posts: 7
Decimal to binary converter
« on: April 23, 2014, 07:43:38 pm »
Solved.
« Last Edit: April 24, 2014, 10:18:43 am by paradox2397 »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Decimal to binary converter
« Reply #1 on: April 23, 2014, 07:58:12 pm »
IMO you need only div 2 and mod 2.

Example: 19

19 mod 2 = 1  .. the most right bit
19 div 2 = 9
9 mod 2 = 1 .. next bit
9 div 2 = 4
4 mod 2 = 0 .. next bit
4 div 2 = 2
2 mod 2 = 0 .. next bit
2 div 2 = 1
1 mod 2 = 1 .. the most left bit
now the result is 1 so the loop can end.
Binary result is 10011.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

paradox2397

  • New member
  • *
  • Posts: 7
Re: Decimal to binary converter
« Reply #2 on: April 23, 2014, 08:32:52 pm »
I know how to convert on paper. We need to divide by 2 until the dividend is less than the divisor(2) and then we need to read the the remainders reversely, and that will be the answer. I don't know how to do this in Pascal...

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Decimal to binary converter
« Reply #3 on: April 23, 2014, 09:25:56 pm »
Sounds like school assignement?

Bart

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Decimal to binary converter
« Reply #4 on: April 23, 2014, 09:26:42 pm »
We usually do not want to write other people's homeworks. :)

So try to write it yourself. If you will not know how to continue, paste here your code and we will help you.

It's not difficult task. One while..do loop should be enough.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

paradox2397

  • New member
  • *
  • Posts: 7
Re: Decimal to binary converter
« Reply #5 on: April 23, 2014, 10:33:14 pm »
Solved.
« Last Edit: April 24, 2014, 10:19:18 am by paradox2397 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Decimal to binary converter
« Reply #6 on: April 23, 2014, 11:17:16 pm »
You have to save the intermediate results at each iteration of the div/mod - you're just thowing them away (and you also start by overwriting your input with an uninitialised variable).
Here's a GUI approach which needs a memo dropped on a form and an OnCreate form handler.

Code: [Select]
unit mainDecimalToBinary;

{$mode objfpc}{$H+}

interface

uses
  Forms, Controls, StdCtrls, sysutils;

type

  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  end;

  // this function writes 8 digits whatever the byte value represented
  function ByteValueToBinaryString(bValue: byte;
                                   addB: boolean=True;
                                   prefix: boolean=False): shortstring;

var
  Form1: TForm1;

implementation

function ByteValueToBinaryString(bValue: byte; addB: boolean; prefix: boolean
  ): shortstring;

const NumberOfBinaryDigitsInAByte=8;

var
  i : integer;
begin
  Result[0]:=Char(NumberOfBinaryDigitsInAByte);
  for i:= NumberOfBinaryDigitsInAByte downto 1 do
   begin
     Result[i]:=Char(Ord('0') + bValue and 1);
     bValue:= bValue div 2;
   end;
  if addB then
    if prefix then
      Result:='b'+Result
    else Result:=Result+'b';
end;

{$R *.lfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  Memo1.Align:=alClient;
  Memo1.ScrollBars:=ssAutoVertical;
  Memo1.Clear;
  for i:= Low(Byte) to High(Byte) do
   Memo1.Lines.Add(Format('%s (%d)',[ByteValueToBinaryString(i), i]));
end;

end.


paradox2397

  • New member
  • *
  • Posts: 7
Re: Decimal to binary converter
« Reply #7 on: April 24, 2014, 10:22:49 am »
Why perplex, howardpc?

Code: [Select]
program test;
uses crt;
var d,x:longint; wh:char; bn:string;

begin
  clrscr;
  write('Enter a decimal number: ');
  readln(d);
  repeat
    if d mod 2 = 0 then wh := '0'
    else wh := '1';
    bn := bn + wh;
    d := d div 2;
  until d = 0;
  for x := lenght(bn) downto 1 do
    write(bn[x]);
  readkey
end.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Decimal to binary converter
« Reply #8 on: April 24, 2014, 11:07:57 am »
Good job.  8)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

 

TinyPortal © 2005-2018