How can I convert base of decimal string to another base? [closed]

送分小仙女□ 提交于 2019-12-14 03:29:41

问题


here is my codes;

{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!!

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?


回答1:


You problem is maximum integer size in Delphi.

MaxInt = 2147483647

MaxInt64 = 9223372036854775807

You number = 12345678912345678998765432101234569870

So definetly it is beyond what Delphi can do.

You may need to use BigInt.

http://sourceforge.net/projects/bigint-dl/



来源:https://stackoverflow.com/questions/19598629/how-can-i-convert-base-of-decimal-string-to-another-base

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!