I have this problem: I have declared a new Type like this:
TmyData = record
adresse: longword;
buffer : string[20];
...
end; (and many others items of different types. everything from byte to
longword mixed up all together)
now I need to make from this record an array of byte or even better an array
of boolean (for every bit of the record).
The order of the items stored inside the record should not be changed.
The only solution I found so far is to add to the bit array every item one
by one what is very tricky and when I change something with the record I
have to change the convert-function as well.
does somebody have a different and easier idea on how to solve this?
thanks
Michael
>now I need to make from this record an array of byte or even better an array
>of boolean (for every bit of the record).
>The order of the items stored inside the record should not be changed.
>The only solution I found so far is to add to the bit array every item one
>by one what is very tricky and when I change something with the record I
>have to change the convert-function as well.
>does somebody have a different and easier idea on how to solve this?
>
If you want an array of the same bytes in memory you can do something like :-
TMyRecord = record
Address : DWord;
Name : string[50];
Age : Byte;
Group : integer;
end;
TRecArray = array[0..SizeOf(TMyRecord) - 1] of byte;
var
MyRecord : TMyRecord;
MyArray : TRecArray absolute MyRecord; // overlays the same memory locations
If you want a copy of the record or array you can do it with
var
MyRecord : TMyRecord;
MyArray : TRecArray
SetLength(MyArray, SizeOfMyRecord);
CopyMemory(MyArray, MyRecord, SizeOf(MyRecord);
Why do you want an array of the bits ? Do you want an array of all the bits (no
different from an array of the bytes). Or do you want an array of boolean which
are changed from false to true when the corresponding byte in the record is
changed - explain a bit more what you are trying to do.
Alan Lloyd
alang...@aol.com
AlanGLLoyd schrieb in Nachricht
<20000318171534...@nso-ck.aol.com>...
Michael,
You can use a dynamic byte array to "overlay" the record, and then access
the data in the record as if it were a series of bytes. Here's some example
code:
type
TmyData = record
address: longword;
buffer : string[20];
end;
procedure TForm1.FormCreate(Sender: TObject);
var
MyData : TMyData;
MyByteArray : array[0..0] of byte absolute MyData;
LengthOfMyByteArray,
ByteIndex : integer;
DisplayString : string;
begin
MyData.Address := 12345;
MyData.Buffer := 'Hello, world!';
LengthOfMyByteArray := SizeOf(MyData) - 1;
DisplayString := '';
for ByteIndex := 0 to LengthOfMyByteArray do
{$R-}
// This technique of accessing data via a dynamic array
// causes range-check errors if range checking is turned on.
// Since the program won't be able to catch out-of-bounds errors,
// it's up to the programmer to do so.
// "walk" the array
DisplayString := DisplayString + IntToStr(MyByteArray[ByteIndex]) + ' ';
ShowMessage(DisplayString);
{$R+}
end;
HTH,
Chris.
---------