// Integer-Zahlen
können in Bitfolgen umgewandelt werden function IntToBin(i: int64): string;
begin
result := '';
while i > 0 do begin
result := inttostr(ord(odd(i))) + result;
i := i shr 1;
end;
if result = '' then result := '0';
end;
function BinToInt(b: string): int64;
var x, lg: integer;
begin
b := trim(copy(b, length(b) - 63, 64));
lg := length(b);
result := 0;
for x := lg downto 1 do
result := result + (strtoint(b[x]) shl (lg - x));
end;
// Beispielaufruf
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then begin
key := #0;
label1.Caption := IntToBin(strtoint(edit1.text));
// und Gegenprobe
label2.Caption := inttostr(BinToInt(label1.caption));
end;
end;
//--------------------------------------------------
// Bei der Umwandlung von Bytes will man meist auch die
// Vornullen anzeigen lassen: 11 => 00001011
function ByteToBin(i: byte): string;
var x: byte;
begin
result := '';
for x := 0 to 7 do begin
result := inttostr(ord(odd(i))) + result;
i := i shr 1;
end;
end;
// Und wieder zurück
type s8 = string[8];
function BinToByte(b: s8): byte;
var x, lg: integer;
begin
lg := length(b);
result := 0;
for x := lg downto 1 do
result := result + (strtoint(b[x]) shl (lg - x));
end;
|
Zugriffe seit 6.9.2001 auf Delphi-Ecke





