{十六进制转换为十进制}
function HexToInt(const aHex: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aHex = '' then Exit;
K := 0;
L := Length(aHex);
for I:=1 to L do
begin
if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then
K := K + Trunc(StrToInt(aHex[I]) * Power(16, L-I))
else case aHex[I] of
'a', 'A' : K := K + Trunc(10 * Power(16, L-I));
'b', 'B' : K := K + Trunc(11 * Power(16, L-I));
'c', 'C' : K := K + Trunc(12 * Power(16, L-I));
'd', 'D' : K := K + Trunc(13 * Power(16, L-I));
'e', 'E' : K := K + Trunc(14 * Power(16, L-I));
'f', 'F' : K := K + Trunc(15 * Power(16, L-I));
end;
end;
{输入一个字符串;输出每个字符的HEX码的一个串}
function StrToHex(const S: string): string;
var
i,ilen: Integer;
wTemp : Word;
begin
Result := '';
if S = '' then Exit;
ilen := Length(S);
for i:=1 to ilen do
begin
wTemp := Ord(S[i]);
Result := Result + IntToStr(wTemp);
end;
end;
{二进制转换为十六进制}
function BinToHex(const aBin: string): string;
var
I,L,K: Integer;
begin
Result := '';
if aBin = '' then Exit;
K := 0;
L := Length(aBin);
for I:=1 to L do
K := K + Trunc(StrToInt(aBin[I]) * Power(2, L-I));
Result := '$' + IntToHex(K, 4);
end;
{二进制转换为十进制}
function BinToInt(const aBin: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aBin = '' then Exit;
K := 0;
L := Length(aBin);
for I:=1 to L do
K := K + Trunc(StrToInt(aBin[I]) * Power(2, L-I));
Result := K;
end;
{十六进制转换为二进制}
function HexToBin(const aHex: string): string;
var
S: string;
I,K: Integer;
begin
Result := '';
if aHex = '' then Exit;
S := ''; Result := '';
K := StrToInt('$'+aHex);
while K>=2 do
begin
S := S + IntToStr(K mod 2);
K := K div 2;
end;
S := S + IntToStr(K);
K := Length(S);
if K<4 then
S := S + Copy('000', 1, 4-K);
K := Length(S);
for I:=K downto 1 do
Result := Result + S[I];
end;
{十六进制转换为十进制}
function HexToInt(const aHex: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aHex = '' then Exit;
K := 0;
L := Length(aHex);
for I:=1 to L do
begin
if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then
K := K + Trunc(StrToInt(aHex[I]) * Power(16, L-I))
else case aHex[I] of
'a', 'A' : K := K + Trunc(10 * Power(16, L-I));
'b', 'B' : K := K + Trunc(11 * Power(16, L-I));
'c', 'C' : K := K + Trunc(12 * Power(16, L-I));
'd', 'D' : K := K + Trunc(13 * Power(16, L-I));
'e', 'E' : K := K + Trunc(14 * Power(16, L-I));
'f', 'F' : K := K + Trunc(15 * Power(16, L-I));
end;
end;
Result := K;
end;
{十进制转换为二进制}
function IntToBin(const aInt: Integer): string;
var
s: string;
i,j: Integer;
begin
s := ''; Result := '';
i := aInt;
while i >= 2 do
begin
s := s + IntToStr(i mod 2);
i := i div 2;
end;
s := s + IntToStr(i);
i := Length(s);
if i < 4 then
s := s + Copy('000', 1, 4-i);
i := Length(s);
for j:=i downto 1 do
Result := Result + s[j];
end;
{十六进制转换为十进制}
function HexToInt(const aHex: string): Integer;
var
I,L,K: Integer;
begin
Result := 0;
if aHex = '' then Exit;
K := 0;
L := Length(aHex);
for I:=1 to L do
begin
if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then
K := K + Trunc(StrToInt(aHex[I]) * Power(16, L-I))
else case aHex[I] of
'a', 'A' : K := K + Trunc(10 * Power(16, L-I));
'b', 'B' : K := K + Trunc(11 * Power(16, L-I));
'c', 'C' : K := K + Trunc(12 * Power(16, L-I));
'd', 'D' : K := K + Trunc(13 * Power(16, L-I));
'e', 'E' : K := K + Trunc(14 * Power(16, L-I));
'f', 'F' : K := K + Trunc(15 * Power(16, L-I));
end;
end;
function HexCharToInt(HexToken : char):Integer;//十六进制字符转十进制
begin
if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
Result:=0;
if (HexToken>#47) and (HexToken<#58) then { chars 0....9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { chars A....F }
Result:=Ord(HexToken)-65 + 10;
end;
function involution(exp:integer):integer; //乘方 指数大于1
var
a:integer;
begin
Result:=1;
if exp<=0 then
exit;
for a:=1 to exp do
Result:=Result*16;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
edit2.Text:=inttostr(involution(strtoint(edit1.Text)));
end;
function HexstrToInt(hexstr : string):Integer; //十六进制字符串转十进制
var
i,temp:integer;
begin
temp:=0;
For i:=1 To length(hexstr) do
temp:=temp+(HexCharToInt(hexstr[i]))* involution(length(hexstr)-i);
Result:=temp;
end;