Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

howTo: convert record into array of byte

2,009 views
Skip to first unread message

Michael Karch

unread,
Mar 18, 2000, 3:00:00 AM3/18/00
to
hi folks

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

AlanGLLoyd

unread,
Mar 18, 2000, 3:00:00 AM3/18/00
to
In article <8b0i24$ke4$1...@news08.btx.dtag.de>, "Michael Karch"
<m.k...@t-online.de> writes:

>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


Michael Karch

unread,
Mar 19, 2000, 3:00:00 AM3/19/00
to
well I need to send this Record (it's a DataPackage) over a serial bus. Not
the com-port, but a "AutoBus", a serial-port working only in my simulation
program. The convert to TBits is for displaying the send or received
DataPackage as a bitmap of dots, you know what I mean? And therefore the
lengths of the string is not constant. If chosen string[20], because the
maximal length of the content field is 20 bytes.
I'll try to do as you wrote and the cutout of the not needed additional
spaces in the content-field should be no problem.
Your guess how the MyRecord would look like is interesting. Address is the
Address of the sender/recipient, group the device-class, buffer the content
etc.. ;-)
if you are interested you can visit www.jufo2000.notrix.de
Thank you for your help

AlanGLLoyd schrieb in Nachricht
<20000318171534...@nso-ck.aol.com>...

Chris R. Timmons

unread,
Mar 19, 2000, 3:00:00 AM3/19/00
to
Michael Karch wrote in message <8b0i24$ke4$1...@news08.btx.dtag.de>...

>hi folks
>
>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

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.
---------

0 new messages