I want to send float values from java to c++ over a socket, without any third party library and byte order safely.
On the C++ side, I handle the byte order (using htonf from the beej's guide). But how can I handle the byte-order in java? For int, the byte order seems handled well by the java socket but float doesn't transmit correct values. Or is conversion to strings the only way to send floats safely?
java:
DataOutputStream out;
out.writeFloat(val);
C++ conversion method from beej's guide:
float ntohf(uint32_t p)
{
float f = ((p>>16)&0x7fff); // whole part
f += (p&0xffff) / 65536.0f; // fraction
if (((p>>31)&0x1) == 0x1) { f = -f; } // sign bit set
return f;
}