Reputation: 6328
I have an array of CameraSpacePoint, which I have converted into bytes in C# programming language. The CameraSpacePoint
is defined as follow:
namespace Microsoft.Kinect
{
public struct CameraSpacePoint
{
public float X;
public float Y;
public float Z;
}
}
To convert the array of CameraSpacePoint
into bytes, I used the following method in C#:
public static byte[] ObjectToByteArray(Object obj)
{
BinaryFormatter bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
The bytes array is then transferred using TCP. I am trying to receive this byte array on the other machine in the following manner:
#include <ros/ros.h>
#include <boost/asio.hpp>
constexpr size_t data_size = 512 * 424;
unsigned char data_buffer[data_size];
boost::asio::read(socket, boost::asio::buffer(data_buffer, data_size));
I declared a similar class in C++ as follow:
class CameraSpacePoint
{
public:
float X;
float Y;
float Z;
};
I want to know that how to convert back byte array into my defined CameraSpacePoint
object of array?
Upvotes: 0
Views: 1050
Reputation: 10868
Memory layout and serialization of objects in C# programming language has nothing to do with similar concepts in C/C++. Having different compilers/OSes, even in a C++ to C++ scenario you may get different memory layouts for same struct or class (according to memory layout, etc.).
What you really need is a common protocol for both languages to serialize and deserialize similar objects. There are some serialization libraries out there which support both languages:
Google's Protocol Buffers Supports both C# and C++, it's platform-neutral. (You can use it to serialize/deserialize between big-endian ARM and little-endian x86_64 for example)
Microsoft Bond Supports both C# and C++
Upvotes: 2