Thema: Delphi 0b0111 in Delphi-Sprache

Einzelnen Beitrag anzeigen

scp

Registriert seit: 31. Okt 2003
1.120 Beiträge
 
Delphi 7 Personal
 
#23

Re: 0b0111 in Delphi-Sprache

  Alt 6. Nov 2003, 19:26
Eine Alternative:

Delphi-Quellcode:
function ByteToBinWord(AByte : Byte) : LongWord;
begin
  result := AByte and $01;

  result := result + (((AByte shr 1) and $01) * 10);
  result := result + (((AByte shr 2) and $01) * 100);
  result := result + (((AByte shr 3) and $01) * 1000);
  result := result + (((AByte shr 4) and $01) * 10000);
  result := result + (((AByte shr 5) and $01) * 100000);
  result := result + (((AByte shr 6) and $01) * 1000000);
  result := result + (((AByte shr 7) and $01) * 10000000);
end;

function BinWordToByte(ABinWord : LongWord) : Byte;
begin
  result := 0;
  result := result + ((ABinWord div 10000000) shl 7); ABinWord := ABinWord mod 10000000;
  result := result + ((ABinWord div 1000000) shl 6); ABinWord := ABinWord mod 1000000;
  result := result + ((ABinWord div 100000) shl 5); ABinWord := ABinWord mod 100000;
  result := result + ((ABinWord div 10000) shl 4); ABinWord := ABinWord mod 10000;
  result := result + ((ABinWord div 1000) shl 3); ABinWord := ABinWord mod 1000;
  result := result + ((ABinWord div 100) shl 2); ABinWord := ABinWord mod 100;
  result := result + ((ABinWord div 10) shl 1); ABinWord := ABinWord mod 10;
  result := result + ABinWord;
end;

function ByteToBinStr(AByte : Byte) : String;
begin
  result := IntToStr(ByteToBinWord(AByte));
  while (length(Result) < 8) do
    result := '0' + result;
end;

function BinStrToByte(ABinStr : String) : Byte;
begin
  result := BinWordToByte(StrToIntDef(ABinStr, 0));
end;
So kann man 8 Nullen und Einsen in einen LongWord packen und wieder als Byte oder String zurückholen.
  Mit Zitat antworten Zitat