将10进制的数字转为n进制的数字

时间:2010-03-05 18:49:33  来源:第二电脑网  作者:第二电脑网

  第二电脑网导读:s; interface uses SysUtils; function Dec_To_Base(nBase, nDec_Value, Lead_Zeros:integer; cOmit:string):string; // 10 进制 -> n 进制 function Base_To_Dec(nBase:integer;cBase_Value, cOmit:string):integer; // n 进制 -> 10 进制 implementation function Dec_To_Base(nBa...
  正文:下面的代码是如何将10进制的数字转为 n 进制的数字。



unit BaseFunctions;

interface

uses
SysUtils;

function Dec_To_Base(nBase, nDec_Value, Lead_Zeros:integer; cOmit:string):string; // 10 进制 -> n 进制
function Base_To_Dec(nBase:integer;cBase_Value, cOmit:string):integer; // n 进制 -> 10 进制


implementation

function Dec_To_Base(nBase, nDec_Value, Lead_Zeros:integer; cOmit:string):string;
{Function : converts decimal integer to base n, max = Base36
Parameters : nBase = base number, ie. Hex is base 16
nDec_Value = decimal to be converted
Lead_Zeros = min number of digits if leading zeros required
cOmit = chars to omit from base (eg. I,O,U,etc)
Returns : number in base n as string}

var
Base_PChar : PChar;
Base_String : string;
To_Del, Modulus, DivNo : integer;
temp_string : string;
i, nLen, Len_Base : integer;
begin
  {initialise..}
  Base_String := 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ; {max = Base36}
  To_Del := 0;
  Modulus := 0;
  DivNo := nDec_Value;
  result := ;
  if (nBase > 36) then nBase := 36; {max = Base36}
  cOmit := UpperCase(cOmit);
  {build string to fit specified base}
  if not(cOmit = ) then begin
    {iterate thru ommited letters}
    nLen := Length(cOmit);
    for i := 1 to nLen do begin
      To_Del := Pos(cOmit, Base_String); {find position of letter}
      if (To_Del > 0) then begin
        {remove letter from base string}
        Len_Base := Length(Base_String);
        temp_string := Copy(Base_String, 0, To_Del - 1);
        temp_string := temp_string + Copy(Base_String,To_Del + 1,Len_Base - To_Del);
        Base_String := temp_string;
      end; {if To_Del>0..}
    end; {for i..}
  end; {if not cOmit=..}
  {ensure string is required length for base}
  SetLength(Base_String, nBase);
  Base_PChar := PChar(Base_String);
  {divide decimal by base & iterate until zero to convert it}
  while DivNo > 0 do begin
    Modulus := DivNo mod nBase; {remainder is next digit}
    result := Base_PChar[Modulus] + result;
    DivNo := DivNo div nBase;
  end; {while..}
  {fix zero value}
  if (Length(result) = 0) then result := 0;
  {add required leading zeros}
  if (Length(result) < Lead_Zeros) then
  for i := 1 to (Lead_Zeros - Length(result)) do result := 0 + result;
end; {function Dec_To_Base}

function Base_To_Dec(nBase:integer;cBase_Value, cOmit:string):integer;
{Function : converts base n integer to decimal, max = Base36
Parameters : nBase = base number, ie. Hex is base 16
cBase_Value = base n integer (as string) to be converted
cOmit = chars to omit from base (eg. I,O,U,etc)
Returns : number in decimal as string}

var
Base_PChar : PChar;
Base_String : string;
To_Del, Unit_Counter : integer;
temp_string : string;
i, nLen, Len_Base : integer;
begin
  {initialise..}
将10进制的数字转为n进制的数字》由第二电脑网原创提供,转载请注明:http://www.002pc.com/master/College/Language/Delphi/13064.html

 1/5    1 2 3 4 5 下一页 尾页


关键字:

关于《将10进制的数字转为n进制的数字》文章的评论

站内搜索: 高级搜索

热门搜索: Windows style 系统 tr IP QQ CPU 安装 function 注册 if td