Convert octal to decimal

How do i convert a octal number to decimal?
LVL 1
lfrodriguesAsked:
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.

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
BatalfCommented:
I don't know any function in Delphi for converting of Octal to Decimals. Maybe a procedure like above could help you.
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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