Search code examples
c#c++byteipc

Convert a float to a byte[] in order to send through a named pipe (C++)


I have a C++ process which has a thread that needs to send floats stored in an array to another process via named pipes. I have constructed the byte array ready for sending but I am not too sure how to get the floats in a form that can be sent over (i.e. how to convert them into bytes).

The other process is a C# process on the same machine and I assume I can use BitConverter to grab the relevant bytes in the incoming byte array and change it into a float but I am not too familiar with C++ and don't know how to change the float to a byte[].

The platform is Windows, I am using Visual Studio 2010 C++.


Solution

  • Simple enough:

    const void *data = &myFloat;
    size_t size = sizeof myFloat;
    

    Then use memcpy to move the data where you want it.