N
N
Nikita2015-10-25 19:33:45
Delphi
Nikita, 2015-10-25 19:33:45

?

                                

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
prishelec, 2015-10-25
@prishelec

Enjoy dear man.
I don't remember what method. For a long time I wrote according to certain rules - I don’t remember the author.

{*******Перевод К-ой в десятичеую степнь******}
function XToDecimal(Value: String; Fn: Byte): LongInt;
var
   Result: Longint;
   i: Integer;
{**************Перевод строки в число*************}
function StrToInt(Index: Char): Integer;
var
  V, Code: Integer;
begin
  Index := UpCase(Index);

  if Index in ['0'..'9'] then
    Val(Index,V,Code);
   if Index in ['A'..'Z'] then
   	V := Ord(Index) - 55;

   StrToInt := V;
end;
{**************************************}
begin
  Result := 0;

   case Length(Value) of
   	1: Result := StrToInt(Value[1]);
    	2: Result := StrToInt(Value[1]) * Fn + StrToInt(Value[2])
    else
      for i := 1 to Length(Value) do
        Result := Result * Fn + StrToInt(Value[i]);
   end;

   XToDecimal := Result;
end;

{***Перевод из десятичной в К-ую систему**}
function DecimalToX(Value: LongInt; Fn: Byte): String;
const
  SizeStr = 255;
var
   Buf: array [1..SizeStr] of Char;
   Result: String;
   i: Integer;
{*********Перевод числа в строку*******}
function IntToStr(Index: Integer): Char;
begin
   case Index of
   	0..9: IntToStr := Chr(Index + 48) ;
      10..36: IntToStr := Chr(Index + 55) ;
  end;
end;
{**************************************}
begin
   i := SizeStr;
   Result := '';

   while (Value <> 0) do
   begin
    Buf[i] := IntToStr(Value mod Fn);
    Value := Value div Fn;
    Dec(i);
   end;

   Result := Buf;
   Delete(Result, 1, i);
   DecimalToX := Result;
end;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question