Structure as byte array

G

Guest

Hi

I am new to C# and am rewritting some C++ code. I want to send a byte array over a serial port. The elements of the byte array are really a structure I have populated. My question is, how do I do this in C# - that is, I want to declare a byte array, assign a structure to it, and then be able to populate the structure's fields, then use the byte array

Thank you
Joe
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

Part of this depends on what the structure is made up of. If the
structure completely contains its elements (no pointers, the arrays are
embedded within the structure), then a nice little trick you can do is to
create an array that is the size of the structure in bytes. Once you do
that, create an array of structures that contains one element. Set the
element in the array of structures to your structure, and then call the
static BlockCopy method on the Buffer class. This will copy the bytes in
the structure to the array.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe Thompson said:
Hi,

I am new to C# and am rewritting some C++ code. I want to send a byte
array over a serial port. The elements of the byte array are really a
structure I have populated. My question is, how do I do this in C# - that
is, I want to declare a byte array, assign a structure to it, and then be
able to populate the structure's fields, then use the byte array.
 
G

Guest

Hi Nicholas

Thank you for the reply. My structure is self contained. I don't understand what you mean by
"call the static BlockCopy method on the Buffer class"
What is the Buffer class? I don't see a method called BlockCopy in C# anywhere..

Thank you
Joe
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

The Buffer class is in the System namespace, and is in the mscorlib.dll
assembly, so it is available to you by default.

The only issue is knowing how large the structure is going to be. You
can determine this programatically by using the sizeof operator in unsafe
code. You might be tempted to use the static SizeOf method on the Marshal
class. I would recommend against that, because the size of the structure in
bytes on the unmanaged end is not always the same as the structure on the
managed end.

You can refer to the sizeof operator documentation to determine the size
of the structure:

http://msdn.microsoft.com/library/d...y/en-us/csspec/html/vclrfcsharpspec_A_5_8.asp

Your code should look like this:

// The structure we want to transform into bytes.
MyStruct pobjStruct = new MyStruct();

// Do some work to set the values. Now initialize the bytes.
// pintNumberOfBytesNeededForStructure is the number of bytes that the
structure takes up in memory.
byte[] pbytBytes = new byte[pintNumberOfBytesNeededForStructure];

// Create an array of the structures, make it one element long, and set to
your struct.
MyStruct[] pobjStructArray = new MyStruct[1];

// Set the value in the structure.
pobjStructAway[0] = pobjStruct;

// Copy the structure into the byte array.
Buffer.BlockCopy(pobjStructArray, 0, pbytBytes, 0,
pintNumberOfBytesNeededForStructure);
 
G

Guest

Hi Nicholas

Maybe I replied to fast. I just ran the code and got the following runtime error on the Buffer.BlockCopy line

Object must be an array of primitive

Thank you
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top