If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

SOS:: Structure Sizes to Byte Arrays!

Hi!

I need to copy a structure into a byte array so that I can send it over a
socket. But I have been having *problems*.

Here is a runthru' of what I'm doing..
I need byte alignment - so I have the attributes defined before the struct.
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=1)]
struct aStruct
{
public int field1;
public int field2;
public int field3;
public int field4;
}

However, when I have to convert the struct into a byte stream for writing to
the socket, I cannot figure out how to convert the struct to a byte array
except by converting the object to a string and then to a byte array - this
way I get the size of the struct as 32 bytes while it should be 16 bytes.