本文介绍了如何将小数字符串的基数转换为另一个基数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码;

{Sayitabani=orginal base, SonucTabani=result-converted base}
function SayiCevir(Sayi:String;const SayiTabani,SonucTabani:Word):String;
 function ChToRkm(const C:Char):Byte;
 var B:Byte absolute C;{c ile b aynı adresteki değişkenlerdir. c:='B' olursa b değeri 66 olur veya b:=65 olursa c değeri 'A' olur}
 begin
   if C>='A' then Result:=B-55 else Result:=B-48;
 end;
 function RkmToCh(B:Byte):Char;
 var C:Char absolute B;
 begin
   if B>9 then B:=B+55 else B:=B+48;
   Result:=C;
 end;
const AltSinir=1; UstSinir=35;
var i,j:Integer; fSayi,Basamak:Int64;
begin
  //if (SayiTabani=SonucTabani) then Result:=Sayi else
  if (SayiTabani<=AltSinir) or (SonucTabani<AltSinir) or (SayiTabani>UstSinir) or (SonucTabani>UstSinir) then
   raise Exception.CreateFmt('%d tabanındaki sayı %d tabanına çevrilmek isteniyor fakat desteklenen taban aralığı %d-%d''dir.',[SayiTabani,SonucTabani,AltSinir,UstSinir])
  else begin
    Sayi:=UpperCase(Trim(Sayi));
    fSayi:=0;Basamak:=1;
    for i:=Length(Sayi) downto 1 do begin
      j:=ChToRkm(Sayi[i]);
      if j>=SayiTabani then raise Exception.CreateFmt('%s sayısı %d tabanlı bir sayı değildir.',[Sayi,SayiTabani]);
      fSayi:=fSayi+(j*Basamak);
      Basamak:=Basamak*SayiTabani;
    end;
    Result:='';
    if fSayi=0 then Result:='0'
    else while fSayi>0 do begin
      Result:=RkmToCh(fSayi mod SonucTabani)+Result;
      fSayi:=fSayi div SonucTabani;
    end;
  end;
end;

我使用此代码将数字转换为其他基数。该代码正常工作,但是数量长度有限制。例如,

Im using this codes for converting base of number to other base. This codes is working normally but there is a limitation of length of number. For example,

I convert:

String:=SayiCevir('123456789',10,11); //Thats OK.

If I wrote:

String:=SayiCevir('12345678912345678998765432101234569870',10,11); //FAIL!!

第二个代码不工作,不返回任何结果。所以;
如何将LONG整数的基数转换为其他基数?我认为问题是String的长度?对吗?

Second codes arent working, dont return any result. So;How can I convert base of "LONG" string of integer to other base? I think problem is Length of String? Right?

推荐答案

您的问题是Delphi中的最大整数大小。

You problem is maximum integer size in Delphi.

MaxInt = 2147483647

MaxInt = 2147483647

MaxInt64 = 9223372036854775807

MaxInt64 = 9223372036854775807

您的号码= 12345678912345678998765432101234569870

You number = 12345678912345678998765432101234569870

所以这绝对是德尔福可以做的。

So definetly it is beyond what Delphi can do.

你可能需要使用BigInt。

You may need to use BigInt.

这篇关于如何将小数字符串的基数转换为另一个基数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 13:10