View Full Version : سوال: بردن اعداد به مبناهای مختلف
  
Mask
سه شنبه 06 مهر 1389, 21:40 عصر
با سلام
آیا تابعی در دلفی هست که بشه هر عددی رو به مبنای دلخواه برد.
ممنون.
Mahmood_M
چهارشنبه 07 مهر 1389, 01:53 صبح
Converting a number from one base to another (http://wall.riscom.net/books/delphi/del_faqs/865.html)
K.Mohammadreza
جمعه 09 مهر 1389, 01:24 صبح
function HexToInt(Str: string): Integer;
var
 intC: Integer;
 intL: Integer;
 chrC: Char;
begin
 Str   := UpperCase(Str);
 Result := 0;
 intL    := Length(Str);
 for intC := intL downto 1 do
  begin
   chrC := Str[intC];
   if(chrC <= 'F')and(chrC >= 'A')then
    Result := Result + (Ord(chrC) - 55)shl((intL - intC) shl 2)
   else
   if(chrC <= '9')and(chrC > '0')then
    Result := Result + (Ord(chrC) - 48)shl((intL - intC) shl 2);
  end;
end;
function BinToInt(Str: string): Integer;
var
 intC: Integer;
 intL: Integer;
 chrC: Char;
begin
 Result := 0;
 intL := Length(Str);
 for intC := intL downto 1 do
  begin
   chrC := Str[intC];
   if chrC = '1' then
    Result := Result + 1 shl (intL - intC);
  end;
end;
function OctalToInt(Str: string): Integer;
var
 intC: Integer;
 intL: Integer;
 chrC: Char;
begin
 Result := 0;
 intL := Length(Str);
 for intC := intL downto 1 do
  begin
   chrC := Str[intC];
   if(chrC >= '0')and(chrC <= '7')then
    Result := Result + (Ord(chrC) - 48)shl((intL - intC) * 3);
  end;
end;
function AsciiToInt(Str: string): integer;
var
 intC: Integer;
 intL: Integer;
 chrC: Char;
begin
 Result := 0;
 intL := Length(Str);
 for intC := intL downto 1 do
  begin
   chrC := Str[intC];
   Result := Result + Ord(chrC)shl((intL - intC) shl 8);
  end;
end;
function IntToBin(intI: Integer): string;
var
 intD: Integer;
 bytV: Byte;
 bolN: Boolean;
begin
 bolN := (intI < 0);
 intI := ABS(intI);
 intD := 0;
 Result := '';
 repeat
  bytV := (intI shr intD) and 1;
   Result := Chr(bytV + 48) + Result;
  Inc(intD);
 until (intI < (1 shl intD));
 if bolN then
  Result := '-' + Result;
end;
function IntToOctal(intI: Integer): string;
var
 intD: Integer;
 bytV: Byte;
 bolN: Boolean;
begin
 bolN := (intI < 0);
 intI := ABS(intI);
 intD := 0;
 Result := '';
 repeat
  bytV := (intI shr intD) and 7;
   Result := Chr(bytV + 48) + Result;
  Inc(intD,3);
 until (intI < (1 shl intD));
 if bolN then
  Result := '-' + Result;
end;
function IntToHex(intI: Integer): string;
var
 intD: Integer;
 bytV: Byte;
 bolN: Boolean;
begin
 bolN := (intI < 0);
 intI := ABS(intI);
 intD := 0;
 Result := '';
 repeat
  bytV := (intI shr intD) and $F;
  if bytV > 9 then
   Result := Chr(bytV + 55) + Result
  else
   Result := Chr(bytV + 48) + Result;
  Inc(intD,4);
 until (intI < (1 shl intD));
 if bolN then
  Result := '-' + Result;
end;
function IntToAscii(intI: Integer): string;
var
 intD: Integer;
 bytV: Byte;
 bolN: Boolean;
begin
 bolN := (intI < 0);
 intI := ABS(intI);
 intD := 0;
 Result := '';
 repeat
  bytV := (intI shr intD) and $ff;
   Result := Chr(bytV) + Result;
  Inc(intD, 8);
 until (intI < (1 shl intD));
 if bolN then
  Result := '-' + Result;
end;
 
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.