function IntToBin(const YourInt:Integer):String;// var iCount,iTmp:Integer; begin SetLength(Result,SizeOf(Integer)*8);//好像与你的要求不一样,32位 //如果要8位,把Integer 换为:Byte吧! iTmp := 1; for iCount := SizeOf(Integer)*8 DownTo 1 do begin iTmp := iTmp Shl 1; if iTmp and YourInt = 1 then Result := Result + '1' else Result := Result + '0'; end; end;
好像反了 iTmp := 1; for iCount := 1 to SizeOf(Integer)*8 do begin if iTmp and YourInt = 1 then Result := Result + '1' else Result := Result + '0'; iTmp := iTmp Shl 1; end;
谢谢大家,问题解决,我在DELPHI下找到函数了。
const cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string; var I, J: Integer; begin Result := ''; I := mNumber; while (I >= mScale) and (mScale > 1) do begin J := I mod mScale; I := I div mScale; Result := cScaleChar[J + 1] + Result; end; Result := cScaleChar[I + 1] + Result; for I := 1 to mLength - Length(Result) do Result := '0' + Result; end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer; var I: Byte; L: Integer; begin Result := 0; L := Length(mDigit); for I := 1 to L do Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) * Trunc(IntPower(mScale, I - 1)); end; { DigitToInt }//demo Caption := IntToDigit(2, 2, 8); ShowMessage(IntToDigit(2, 117, 8));
var
iCount,iTmp:Integer;
begin
SetLength(Result,SizeOf(Integer)*8);//好像与你的要求不一样,32位
//如果要8位,把Integer 换为:Byte吧!
iTmp := 1;
for iCount := SizeOf(Integer)*8 DownTo 1 do
begin
iTmp := iTmp Shl 1;
if iTmp and YourInt = 1 then Result := Result + '1'
else Result := Result + '0';
end;
end;
iTmp := 1;
for iCount := 1 to SizeOf(Integer)*8 do
begin
if iTmp and YourInt = 1 then Result := Result + '1'
else Result := Result + '0';
iTmp := iTmp Shl 1;
end;
cScaleChar = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';function IntToDigit(mNumber: Integer; mScale: Byte; mLength: Integer = 0): string;
var
I, J: Integer;
begin
Result := '';
I := mNumber;
while (I >= mScale) and (mScale > 1) do begin
J := I mod mScale;
I := I div mScale;
Result := cScaleChar[J + 1] + Result;
end;
Result := cScaleChar[I + 1] + Result;
for I := 1 to mLength - Length(Result) do Result := '0' + Result;
end; { IntToDigit }function DigitToInt(mDigit: string; mScale: Byte): Integer;
var
I: Byte;
L: Integer;
begin
Result := 0;
L := Length(mDigit);
for I := 1 to L do
Result := Result + (Pos(mDigit[L - I + 1], cScaleChar) - 1) *
Trunc(IntPower(mScale, I - 1));
end; { DigitToInt }//demo
Caption := IntToDigit(2, 2, 8);
ShowMessage(IntToDigit(2, 117, 8));