Mathematics

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;

Convert octal to binary

Posted in Mathematics

Use OctToBin function to convert number in octal format to binary format.

function OctToBin(OctStr: string): string;

  function DecToBinStr(N: Integer): string;
  var
    S: string;
    i: Integer;
  begin
    if N<>0 then
      for i:=1 to SizeOf(N)*8 do
      begin
        if N1) do
        Delete(Result, 1, 1);
  end;
end;

Convert mm to inch and back

Posted in Mathematics

We know, that one inch contains 25.4 mm. So, to convert mm to inch, we should made division of value and 25.4. To convert inch to mm, we should made multiplication of value and 25.4. For example:

// MmToInch
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption:=FloatToStr(StrToFloat(Edit1.Text)/25.4);
end;

// InchToMm
procedure TForm1.Button2Click(Sender: TObject);
begin
  Label2.Caption:=FloatToStr(StrToFloat(Edit1.Text)*25.4);
end;

Convert hexadecimals to decimal

Posted in Mathematics

This algorithm shows a converting hexadecimal value to the decimal value.

function HexToDec(Str: string): Integer;
var
  i, M: Integer;
begin
  Result:=0;
  M:=1;
  Str:=AnsiUpperCase(Str);
  for i:=Length(Str) downto 1 do
  begin
    case Str[i] of
      '1'..'9': Result:=Result+(Ord(Str[i])-Ord('0'))*M;
      'A'..'F': Result:=Result+(Ord(Str[i])-Ord('A')+10)*M;
    end;
    M:=M shl 4;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Edit1.Text<>'' then
    Label2.Caption:=IntToStr(HexToDec(Edit1.Text));
end;

Convert hexadecimal to binary

Posted in Mathematics

Hexadecimal format of a number is a reduced binary format of this number. Use HexToBin function to convert hexadecimal to binary format.

function HexToBin(HexStr: string): string;
const
  BinArray: array[0..15, 0..1] of string =
    (('0000', '0'), ('0001', '1'), ('0010', '2'), ('0011', '3'),
     ('0100', '4'), ('0101', '5'), ('0110', '6'), ('0111', '7'),
     ('1000', '8'), ('1001', '9'), ('1010', 'A'), ('1011', 'B'),
     ('1100', 'C'), ('1101', 'D'), ('1110', 'E'), ('1111', 'F'));
  HexAlpha: set of char = ['0'..'9', 'A'..'F'];
var
  i, j: Integer;
begin
  Result:='';
  HexStr:=AnsiUpperCase(HexStr);
  for i:=1 to Length(HexStr) do
    if HexStr[i] in HexAlpha then
    begin
      for j:=1 to 16 do
        if HexStr[i]=BinArray[j-1, 1] then
          Result:=Result+BinArray[j-1, 0];
    end
    else
    begin
      Result:='';
      ShowMessage('This is not hexadecimal number');
      Break;
    end;
  if Result<>'' then
   while (Result[1]='0')and(Length(Result)>1) do
     Delete(result, 1, 1);
end;