Suppose there is already written, unchangeable host program, that receives such C++ struct by socket:
#pragma pack(push, 2)
struct Data {
double x;
double y;
double z;
long frameNumber;
};
#pragma pack(pop)
platform: C++ / 32-bit Windows Application compiled in Visual Studio 2008
How to send such data from Java Socket? I have attemted to fill ByteBuffer with putDouble(), and putLong(), also putInt() assuming long as 32-bit, but i cannot produce valid values.
I have also generated and sended data randomly, and structure-level bytes assignment looks fine(I can randomize only one value, for example X), but I cannot produce exact value (propably different double representation?), but only random stuff by sending Math.random() * Double.MAX_VALUE;
Can I use Google Protocol Buffers on only one side (client producing data) to solve this problem?
remember, I cannot change the server (receiving) side
I know that I can move sending data to C++ and use JNI, but I'm searching for simpler solution.
I see at least 2 possible problems here:
- double representation at byte-level (like 01234567 and 76543210 on other side)
- bits representation in one byte (like 00000001 and 1000000 on other side)
What those things look in Java in C++?
In several hours I will provide exact sample data for "byte-hacking" (exact Java value before send and value of received double in C++)
About server bad-design answering
I know that such implemented and unchangeable server is bad software, but the environment in this problem is to inject some data to small old application at "hobbistic-level"
Java representation
Maybe this would help some bits-level C++ expert:
Long.toBinaryString( Double.doubleToRawLongBits( (double) 0 ) );
// 000000000000000000000000000000000000000000000000000000000000000
Long.toBinaryString( Double.doubleToRawLongBits( 1 ) );
// 011111111110000000000000000000000000000000000000000000000000000
Long.toBinaryString( Double.doubleToRawLongBits( 1.1 ) );
// 011111111110001100110011001100110011001100110011001100110011010
Long.toBinaryString( Double.doubleToRawLongBits( 1024 ) );
// 100000010010000000000000000000000000000000000000000000000000000
Long.toBinaryString( Double.doubleToRawLongBits( 1024.1024 ) );
// 100000010010000000000000110100011011011100010111010110001110001
Long.toBinaryString( Double.doubleToRawLongBits( Double.MAX_VALUE ) );
// 111111111101111111111111111111111111111111111111111111111111111