利用BCD的算法uses Math function BinaryToInt(InStr:String):Integer; var Counter:Integer; begin Result:=0; for Counter:= 1 to Length(Instr) do Result:=Result+Trunc(StrToInt(InStr[Counter])*Power(2,Length(InStr)-Counter)); end;
二进制的每一位就表示是否相加相应的数值 [code=BatchFile] [] [] [] [] [] +1 +2 +4 +8 +16..... [/code]参考如下代码 var S: string; I, J: Integer; vInteger: Integer; begin S := '11101010101010'; // 二进制 vInteger := 0; J := 1; for I := Length(S) downto 1 do begin if S[I] = '1' then vInteger := vInteger or J; J := J shl 1; end; Caption := IntToStr(vInteger); end;
二进制用byte类型啊,8位 十六进制用Word、DWord、Integer都行,Word是2个字节的,Dword和integer都是4个字节的 赋值时直接用十六进制值赋值,给你几个函数,对你应该有用楼主作什么的?交流一下: [email protected]{输入一个字符串;输出每个字符的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;
var
Counter:Integer;
begin
Result:=0;
for Counter:= 1 to Length(Instr) do
Result:=Result+Trunc(StrToInt(InStr[Counter])*Power(2,Length(InStr)-Counter));
end;
[code=BatchFile]
[] [] [] [] []
+1 +2 +4 +8 +16.....
[/code]参考如下代码
var
S: string;
I, J: Integer;
vInteger: Integer;
begin
S := '11101010101010'; // 二进制
vInteger := 0;
J := 1;
for I := Length(S) downto 1 do
begin
if S[I] = '1' then vInteger := vInteger or J;
J := J shl 1;
end;
Caption := IntToStr(vInteger);
end;
十六进制用Word、DWord、Integer都行,Word是2个字节的,Dword和integer都是4个字节的
赋值时直接用十六进制值赋值,给你几个函数,对你应该有用楼主作什么的?交流一下: [email protected]{输入一个字符串;输出每个字符的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;