Convert octal to decima

Posted in Mathematics

Use function OctToDec to convert number in octal format to decimat format. It is not very difficult, because 217 in octal is 2*8*8+1*8+7 in decimal format.

function OctToDec(OctStr: string): string;
var
  DecNum: Real;
  i: Integer;
  Error: Boolean;
begin
  DecNum:=0;
  Error:=False;
  for i:=Length(OctStr) downto 1 do
  begin
    if not (OctStr[i] in ['0','1','2','3','4','5','6','7']) then
    begin
      Error:=True;
      ShowMessage('This is not octal number');
      Break;
    end;
    DecNum:=DecNum+StrToInt(OctStr[i])*Power(8, Length(OctStr)-i);
  end;
  if not Error then
    Result:=FloatToStr(DecNum)
  else Result:='';
end;