Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi boolarrtostr ??? (https://www.delphipraxis.net/10010-boolarrtostr.html)

braingrenade 9. Okt 2003 14:47


boolarrtostr ???
 
Moin leute !!

Ich möchte gerne ein boolarr
Delphi-Quellcode:
type Tboolarr = array of Boolean ;
in einen string umwandeln also zb. 00101010100111111000001010100101 in 'xyz' .
ich hab mir auch schon ne function geschrieben , welche aber als result immer '' ausgiebt .
Delphi-Quellcode:
function bintobyte(const AsValue : string) : byte;
//Danke an Daniel B für diese Funktion !!!!
const
  _aBinDigits = ['0','1'];

var
  iPowerOfTwo : integer;
  i          : integer;

begin
  Result     := 0;
  iPowerOfTwo := 1;
  if (length(AsValue) < 1) or (length(AsValue) > 8) then
  begin
    raise Exception.Create('Die Zahl muss zwischen 1 und 8 Stellen haben.');
  end;
  for i := length(AsValue) downto 1 do
  begin
    if not (AsValue[i] in _aBinDigits) then
    begin
      raise Exception.Create('Es dürfen nur die Ziffern 0 und 1 vorkommen.');
    end;
    if AsValue[i] = '1' then
    begin
      Result := Result or iPowerOfTwo;
    end;
    iPowerOfTwo := iPowerOfTwo shl 1;
  end;
end;

//in diser Funktion liegt der Fehler
function Tbitcrypt.boolarrtostr(ba : Tboolarr): string;
var i,i2 : integer;
    buf,b : string;
begin
result := '';
for i := 0 to high(ba) do
begin
 if ba[i] = true then buf := buf +'1'
 else buf := buf + '0';
end;

i:=1;
i2:=0;
repeat
 if i2 = 8 then
  begin
   i2 :=0;
   result := result+ char(bintobyte(b));
   b := '';
  end;
 b := b+buf[i];
 i2 := i2 +1;
 i:=i+1;
until i = length(buf)+2;
end;
Weiß jemand woran das liegen könnte ?

Honie 9. Okt 2003 15:06

Re: boolarrtostr ???
 
soweit wie ich es sehe, solltest Du auf jedenfall buf initilisieren.

Also:

buf := ''

am Anfang.

Ansonsten kann ich durch den unstrukturierten Code nicht
so schnell durchblicken.

braingrenade 9. Okt 2003 15:25

Re: boolarrtostr ???
 
Mion honie , leider funzt immer noch nich

sakura 9. Okt 2003 15:36

Re: boolarrtostr ???
 
Du musst sowohl Buf auf '' initialisieren (wichtig spätestens ab dem 2. Aufruf) und

Delphi-Quellcode:
Result := Buf;
;-)

...:cat:...

braingrenade 9. Okt 2003 15:47

Re: boolarrtostr ???
 
Ich glaub ich steh grad auf dem Schlauch !!
Meint ihr das so :
Delphi-Quellcode:
function Tbitcrypt.boolarrtostr(ba : Tboolarr): string;
var i,i2 : integer;
    buf,b : string;
begin
buf:=''; // das is neu
for i := 0 to high(ba) do
begin
 if ba[i] = true then buf := buf +'1'
 else buf := buf + '0';
end;
result := '';
b:=''; // und das is neu
i:=1;
i2:=0;
repeat
 if i2 = 8 then
  begin
   i2 :=0;
   result := result+ char(bintobyte(b));
   b := '';
  end;
 b := b+buf[i];
 i2 := i2 +1;
 i:=i+1;
until i = length(buf)+2;
end;
?

sakura 9. Okt 2003 16:04

Re: boolarrtostr ???
 
Ich habe die Proc einfach mal neu geschrieben, und ich denke mal, die tut jetzt, was Du willst. Die ist aber weder auf Speed noch auf Schönheit optimiert, sollte aber logisch nachzuvollziehen sein ;-)

Dieses Array [011110000111100101111010] würde dann 'xyz' ergeben ;-)

Delphi-Quellcode:
function Boolarrtostr(BA: Tboolarr): string;
var
  TempByte: Byte;
  ArrayPos, Count8Bits, MaxBits: Integer;
begin
  Result := '';
  if Length(BA) = 0 then
    Exit;
  MaxBits := High(BA);
  TempByte := 0;
  Count8Bits := 0;
  ArrayPos := Low(BA);
  while ArrayPos <= MaxBits do
  begin
    TempByte := TempByte shl 1;
    if BA[ArrayPos] then
      TempByte := TempByte or 1;
    Inc(Count8Bits);
    if Count8Bits = 8 then
    begin
      Result := Result + Chr(TempByte);
      TempByte := 0;
      Count8Bits := 0;
    end;
    Inc(ArrayPos);
  end;
  if Count8Bits > 0 then
    Result := Result + Chr(TempByte);
end;
...:cat:...

braingrenade 9. Okt 2003 16:12

Re: boolarrtostr ???
 
Wow ! :shock:

Danke für die schnelle Hilfe!

sakura 10. Okt 2003 12:10

Re: boolarrtostr ???
 
Ich habe obige Version noch um eine Sicherheitsabfrage und eine kleine Feinheit erweitert, damit es nicht zu Fehlern kommt :mrgreen:

...:cat:...


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:33 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz