16进制以下任意转换

进制的算法:

 
program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

//适用于16进制以下转换

function Convert(N: Integer; M: Integer):string;
var
  NumArray: array[1..15] of char;
  I: Integer;
begin
    Result := '';
       for I := 1  to 15 do
       begin
         if(i < 10) then
           NumArray[i] := chr(48 + i mod 10) // 1..9
         else
           NumArray[i] := chr(65 + i mod 10);//A..F
       end;//end for

    while N > 0 do
    begin
        Result := NumArray[N mod M] + Result;
        N := N div M;
    end;


end;

var
  n,m:Integer;
begin
  try
   repeat

    Write('要转换的数值:');
    Readln(n);
    Write('要转成的进制数');
    ReadLn(m);
    WriteLn('转换后:',Convert(n,m));
   until (n < 0) or (m <= 0);
    ReadLn;

  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
posted @ 2012-02-04 18:05  TryHard  阅读(301)  评论(0编辑  收藏  举报