چه جوری یه عدد Hex را به Integer تبدیل کنم
آیا تابع مستقیم برای این کار وجود داره یا باید خودم بسازمش
Printable View
چه جوری یه عدد Hex را به Integer تبدیل کنم
آیا تابع مستقیم برای این کار وجود داره یا باید خودم بسازمش
HexStr : string;
Num : int;
HexStr := '$AFBF12';
Num := StrToInt(HexStr);
با سلام توابع زير مشکلتو حل مي کنن
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;