I have a program that receives network packets that are binary serialized. I currently have a way of storing the data directly from the byte array into the struct by way of using pointers. But what I need now is a way to store UTF strings in the struct as well whilst still maintaining the easy method of copying from the byte array. The string is assumed to be prefixed with an unsigned short indicating the length in bytes of the string. Is this how C# internally stores char[] or strings or do I need to make my own struct to store it in?
My current struct layout is like this:
public struct GenericPacket
{
public short packetid;
public static GenericPacket ReadUsingPointer(byte[] data)
{
unsafe
{
fixed (byte* packet = &data[0])
{
return *(GenericPacket*)packet;
}
}
}
}
Read using pointer simply takes a byte array and returns a pointer to it but as a GenericStruct. This works for every normal types, but strings are more complex.
Thanks for any ideas!