Search code examples
c++serializationcharbytelong-double

How to convert long double to char[12]?


Given long double, I need to serialize it to char[12] (optimally) and deserialize. Is it even possible?


Solution

  • From what I understand of your question, I would do:

    double in=2.132;
    char arr[12] = {};
    memcpy(arr,&in,sizeof(in));
    
    char arr2[12] = ...;
    double out;
    memcpy(&out,arr2,sizeof(out));
    

    assuming endianness and size of double are the same on both sides of the de/serialization.