Recent

Author Topic: Operations with n Base Numbers  (Read 5135 times)

typo

  • Hero Member
  • *****
  • Posts: 3051
Operations with n Base Numbers
« on: September 02, 2010, 08:23:46 pm »
Code: [Select]
uses StrUtils;

const
   ValidChars :string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function IncNumb(Numb :string; Len, Base :byte):string;
var
  i :integer;
begin
  Result := Numb;
  if (Result = '') or (Base < 1) or (Base > 36) then Exit;
  for i := 1 to Length(Result) do
    if not (Result[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  Result  := Dec2Numb(Numb2Dec(Result, Base) + 1, Len, Base);
end;

function IncNumb(Numb, Count :string; Len, Base :byte):string;
var
  i :integer;
begin
  Result := '';
  if (Numb = '') or (Count = '') or (Base < 1) or (Base > 36) then Exit;
  for i := 1 to Length(Numb) do
    if not (Numb[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  for i := 1 to Length(Count) do
    if not (Count[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  Result  := Dec2Numb(Numb2Dec(Numb, Base) + Numb2Dec(Count, Base), Len, Base);
end;

function DecNumb(Numb :string; Len, Base :byte):string;
var
  i :integer;
begin
  Result := Numb;
  if (Result = '')or (Base < 1) or (Base > 36) then Exit;
  for i := 1 to Length(Result) do
    if not (Result[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  Result  := Dec2Numb(Numb2Dec(Result, Base) - 1, Len, Base);
  if Result = '' then Result := Numb;
end;

function DecNumb(Numb, Count :string; Len, Base :byte):string;
var
  i :integer;
begin
  Result := '';
  if (Numb = '') or (Count = '') or (Base < 1) or (Base > 36) then Exit;
  for i := 1 to Length(Numb) do
    if not (Numb[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  for i := 1 to Length(Count) do
    if not (Count[i] in [ValidChars[1]..ValidChars[Base]]) then Exit;
  Result  := Dec2Numb(Numb2Dec(Numb, Base) - Numb2Dec(Count, Base), Len, Base);
end;
« Last Edit: September 02, 2010, 08:31:30 pm by typo »

Silvio Clécio

  • Guest
Re: Operations with n Base Numbers
« Reply #1 on: September 02, 2010, 08:59:43 pm »
Thanks men. ;)

 

TinyPortal © 2005-2018