I have a struct in C++ which has a char[10] field.
struct Package
{
char str[10];
};
I convert the struct into char array and send it to and c# application over a TCP socket and there I convert it back to a c# struct.
[StructLayout(LayoutKind.Sequential)]
public struct Package
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
public string str;
};
The convertion is done properly and I get the message but the problem is when the length of the message is less than the size of the array and I think that it's due to the null terminator in c++ char array.
For instance if the I send "Hello\0" from C++ the char array is something like:
H e l l o \0 \0 \0 \0 \0
And when I get it in c# application it is something like:
H e l l o Ì Ì Ì Ì Ì
And I really don't know what to do with this (personally like to call) junk character 'Ì'.
please help me on this. Any help is appreciated.
Update:
I simply cast the struct to char* and send it over the socket;
Package pkg;
strcpy_s(pkg.str, 'Hello\0');
char* bytes = (char*)&pkg;
send(socket, bytes, sizeof(pkg), NULL);