Convert octal to decimal

How do i convert a octal number to decimal?
LVL 1
lfrodriguesAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
I don't know any function in Delphi for converting of Octal to Decimals. Maybe a procedure like above could help you.
0
 
BatalfCommented:
This is a unit with an example of a procedure which could help

Ad to edit-compnents and one button to a form :

Here's the code :

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  octal : string;
  decimalstring : string;
  decimal : longint;
  i : integer;
  tempnumber : string[1];
  lngth : longint;
  mult : longint;
begin
    octal:=edit1.text;
    lngth := length(octal);
    mult:=1;
    decimal:=1;
    for i:=1 to lngth-1 do
      mult:=mult*8;
    for i:=1 to lngth do
    begin
         tempnumber:=copy(octal,i,1);
         decimal:=decimal+(strtoint(tempnumber)*mult);
         mult:=trunc(mult/8);
    end;
     decimal:=decimal-1;
    decimalstring:=inttostr(decimal);
    edit2.text:=decimalstring;

end;

end.

Regards
Batalf
0
 
BatalfCommented:
This is a unit with an example of a procedure which could help

Ad to edit-compnents and one button to a form :

Here's the code :

unit Unit1;

interface

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

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  octal : string;
  decimalstring : string;
  decimal : longint;
  i : integer;
  tempnumber : string[1];
  lngth : longint;
  mult : longint;
begin
    octal:=edit1.text;
    lngth := length(octal);
    mult:=1;
    decimal:=1;
    for i:=1 to lngth-1 do
      mult:=mult*8;
    for i:=1 to lngth do
    begin
         tempnumber:=copy(octal,i,1);
         decimal:=decimal+(strtoint(tempnumber)*mult);
         mult:=trunc(mult/8);
    end;
     decimal:=decimal-1;
    decimalstring:=inttostr(decimal);
    edit2.text:=decimalstring;

end;

end.

Regards
Batalf
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
lfrodriguesAuthor Commented:
thanks man
0
 
lfrodriguesAuthor Commented:
How do I convert the integer back to octal?
0
 
BatalfCommented:
Hi

Give me some time and I would give you the source code for this, that means extend the program above to also include conversion from octal to numbers.

It a little bit more complex than converting octal numbers to decimals :

As far as I know each digit must be diverted by 8, and we spare the reminders :

ex : decimal 174

174 /8 = 21(dec. deleted), 21 times 8 = 168, 174-168 = 6
21/8 = 2(dec. deleted), 2 times 8 = 16, 21-16 = 5
2/8 = 0, 0 times 8 = 0, 2-0 = 2

The reminders was 2, 5 and 6 = octal number 256

Well, I get back to you

Regards
BATALF

0
 
BatalfCommented:
Hi I have the code here :
I think you could add a new button to your existing form with caption (Dec2Oct) or something like that

I would appreciate if you would offer me some points for this code. (ex : post a question with subject xxx points to Batalf). Do what you feel like :-)

Well, here's the code, I have tested that it's working :

procedure TForm1.Button1Click(Sender: TObject);
type
        remindertype = array[1..10] of byte;
var
 octal : string;
  decimalstring : string;
  reminder:remindertype;
 
  rest : integer;
  tempno : integer;
  remno : integer;
  divid : integer;


begin
    decimalstring:=edit1.text;
    lngth:=length(decimalstring); {counts numbers of digits in octal number}

    remno:=1;
    rest:=strtoint(decimalstring);
    REPEAT
          divid := trunc(rest/8);
      tempno := divid*8;
      reminder[remno] := rest - tempno;
      rest := divid;
      inc(remno);
    UNTIL REST=0;
    dec(remno);
    decimalstring:='';
    repeat
        decimalstring:=decimalstring+inttostr(reminder[remno]);
        dec(remno);
    until remno=0;
    edit2.text:=decimalstring;
end;


Best Regards
Batalf
0
All Courses

From novice to tech pro — start learning today.