{输入一个字符串;输出每个字符的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 := '此处换成美元符号,TMD,直接用发表出来就乱了!' + 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('此处换成美元符号,TMD,直接用发表出来就乱了!'+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;
进制转换
最新推荐文章于 2023-02-08 23:28:26 发布