//2-10
funtion BintoInt(value:string):longint;
var
i,size:integer;
begin
result:=0;
size:=length(value);
for i:=size downto 0 do
begin
if copy(value,i,1)='1' then
begin
result:=result+(1 shl i);
end;
//10- 16
hexstring:=format('%ox',decvalue);
先把二进制转化为十进制整数,然后用IntToHex将十进制整数转化为十六进制。
二进制转化为十进制可参考下面的函数:
function BinStrToInt(BinStr: string): int64;
var counter: integer;
begin
if length(BinStr) >= 64 then { HexStr < 2^63 }
raise ERangeError.Create(#13 + BinStr + #13 +
'is not within the valid range of a 64 bit binary.' + #13);
Result := 0;
for counter := 1 to length(BinStr) do
if BinStr[Counter] = '1' then
Result := Result + pow(2, length(BinStr) - counter);
end;
function pow(base, power: integer): int64;
var counter: integer;
begin
Result := 1;
for counter := 1 to power do
Result := Result * base;
end;
这是我做美萍密码探测器用的几个函数
//整形转二进制字符串
function TForm1.inttobcd(intnum:integer;bcdlen:integer):string;
//intnum代表要转换的整形数.bcdlen代表代表转换后二进制的位数.
BEGIN
RESULT:='';
while INTNUM<>0 do
begin
RESULT:=inttostr(INTNUM mod 2)+RESULT;
INTNUM:=INTNUM div 2
end;
while length(RESULT)<BCDLEN do RESULT:='0'+RESULT;
END;
//二进制字符串转整形数
function TForm1.BCDtoINT(bcdSTR:STRING):INTEGER;
VAR I,I2,ADDNUM:INTEGER;
BEGIN
result:=0;
FOR I:=1 TO LENGTH(bcdSTR) DO
BEGIN
IF (I=LENGTH(bcdSTR)) AND (bcdSTR[LENGTH(bcdSTR)]='1') THEN
BEGIN
RESULT:=RESULT+1;
BREAK;
END;
IF bcdSTR='1' THEN
BEGIN
ADDNUM:=1;
FOR I2:=i TO LENGTH(bcdSTR)-1 DO ADDNUM:=ADDNUM*2;
RESULT:=RESULT+ADDNUM;
END;
END;
END;