符号付き整数型を二進数に変換します。
(ほぼこのサイトからコピペした内容で、
自分がメモしたい内容を文章として残りたいだけです。)
mrxray.on.coocan.jp
2進数⇔8進数⇔10進数⇔16進数 計算・変換ツール
https://note.cman.jp/convert/bit/
内容
function Int32ToBitStr(AInt: Int32): string
//----------------------------------------------------------------------------
// 32 ピットの整数型の値を 2 進数の文字列に変換する関数
//----------------------------------------------------------------------------
function Int32ToBitStr(AInt: Int32): string;
const
LBIT64_1 : UInt64 = 1;
var
LDigit : Integer;
LBitText : string;
LIndex : Integer;
begin
LDigit := SizeOf(AInt) * 2; //ビット数、2だったら、0000 0000って感じ
LBitText := '';
for LIndex := 0 to LDigit - 1 do begin
if (AInt and (LBIT64_1 shl LIndex)) > 0 then begin
LBitText := '1' + LBitText;
end else begin
LBitText := '0' + LBitText;
end;
// 4 ビットごとに文字等を挿入
if (((LIndex + 1) and 3) = 0) and ((LIndex + 1) < LDigit) then begin
LBitText := ' ' + LBitText; //0000 0000
end;
end;
Result := LBitText;
end;
//伝説のボタン1
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Add(Int32ToBitStr(StrToInt(Edit1.Text)));
end;