-2

I have above statement in file I am refering . Expected output is double. I could not find anything relevant to my problem. I found this Passing a structure through Sockets in C but dont know if its relevant. I am not reading that int64 value. I am getting it from other process and that is the way it is designed.

Does anyone have any theory about serialization and deserialization of ints?

Community
  • 1
  • 1

2 Answers2

1

There is exactly one defined way to bitwise-copy one type into another in c++ - memcpy.

template<class Out, class In, std::enable_if_t<(sizeof(In) == sizeof(Out))>* = nullptr>
Out mangle(const In& in)
{
    Out result;
    std::memcpy(std::addressof(result), std::addressof(in), sizeof(Out));
    return result;
}


int main()
{
    double a = 1.1;
    auto b = mangle<std::uint64_t>(a);
    auto c = mangle<double>(b);

    std::cout << a << " " << std::hex << b << " " << c << std::endl;
}

example output:

1.1 3ff199999999999a 1.1
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
-3

How about reading that 64-bit number and using reinterpret_cast to convert it to bitwise equivalent floating point number.

int64_t a = 121314;
double b = *reinterpret_cast<double*>(&a);
int64_t c = *reinterpret_cast<int64_t*>(&b);
assert(a==c);
vre
  • 6,041
  • 1
  • 25
  • 39