I am sending data between Java and iPhone/objC clients. The Java client has an established middleware component that I am using to test integration of the new client to the middleware.
I have a problem with all byte shift operations. The Java code is in production and can not be modified. Since the double seems to be the most extensive I will post it.
To send from objC:
-(void)putDouble:(NSNumber *)v{
unsigned long long n = [v unsignedLongLongValue];
dataToSend = [NSMutableData data];
long long i = (int)n & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(n)]];
i = ((int)n >> 8) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(n)]];
i = ((int)n >> 16) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(n)]];
i = ((int)n >> 24) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(n)]];
i = ((int)n >> 32) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(i)]];
i = ((int)n >> 40) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(i)]];
i = ((int)n >> 48) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(i)]];
i = ((int)n >> 56) & 0x0ff;
[dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(i)]];
[self send:dataToSend];
}
Java receives:
/*
* Retrieve a double (64-bit) number from the stream.
*/
private double getDouble() throws IOException
{
byte[] buffer = getBytes(8);
long bits =
((long)buffer[0] & 0x0ff) |
(((long)buffer[1] & 0x0ff) << 8) |
(((long)buffer[2] & 0x0ff) << 16) |
(((long)buffer[3] & 0x0ff) << 24) |
(((long)buffer[4] & 0x0ff) << 32) |
(((long)buffer[5] & 0x0ff) << 40) |
(((long)buffer[6] & 0x0ff) << 48) |
(((long)buffer[7] & 0x0ff) << 56);
return Double.longBitsToDouble(bits);
}
When I send [[WVDouble alloc]initWithDouble:-13456.134] from objC
java gets double 5.53E-322
The problem is on the objC side, since the java is in production with other development environments. With all production clients -13456.134 is the converted result.
Here is the sendDouble code the java client uses: `
// Write a double (64-bit) number to the stream.
private void putDouble(double number) throws IOException
{
long n = Double.doubleToLongBits(number);
// have to write these in reverse order to be comptible
stream.write((int)(n) & 0x0ff);
stream.write((int)((n >>> 8)) & 0x0ff);
stream.write((int)((n >>> 16)) & 0x0ff);
stream.write((int)((n >>> 24)) & 0x0ff);
stream.write((int)((n >>> 32)) & 0x0ff);
stream.write((int)((n >>> 40)) & 0x0ff);
stream.write((int)((n >>> 48)) & 0x0ff);
stream.write((int)((n >>> 56)) & 0x0ff);
}
//--------------------------------------------------------------------------------
`