Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3324214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:23:42+00:00 2026-05-17T23:23:42+00:00

I am sending data between Java and iPhone/objC clients. The Java client has an

  • 0

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);
}

//--------------------------------------------------------------------------------

`

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:23:43+00:00Added an answer on May 17, 2026 at 11:23 pm

    As @Vovanium pointed out, you’re getting the long long value of the double, which for your example will return -13456. No fraction, no exponent.

    In your shift and mask operations you’re casting to an int too early. You need to apply the cast after the shift and mask. Also, wrapping it up in a loop reduces the mount of code to change.

    int i;
    int j;
    
    for (j = 0; j < sizeof(n); j++) {
        i = (int)(n >> (j*8)) & 0x0ff;
        [dataToSend appendData:[NSMutableData dataWithBytes:&i length:sizeof(i)]];
    }
    [self send:dataToSend];
    

    Do note that, according to the JavaDocs for Double, Java expects the bits will have a particular order. You will get incorrect values if casting from double to unsigned long long does not produce that bit order. In that case it may be necessary to rearrange the bits before sending.

    Bit 63 (the bit that is selected by the mask 0x8000000000000000L) represents the sign of the floating-point number. Bits 62-52 (the bits that are selected by the mask 0x7ff0000000000000L) represent the exponent. Bits 51-0 (the bits that are selected by the mask 0x000fffffffffffffL) represent the significand (sometimes called the mantissa) of the floating-point number.

    update:

    Make sure you pick up @Vovanium’s change so you’re working on the right set of bits:

    double nd = [v doubleValue];
    unsigned long long n = *(long long *)&nd;
    

    Try testing with a value of -1.0. The IEEE 754 bit pattern for a -1.0 is:

    0xbff0000000000000
    

    And, when serialized, the bytes would be

    00 00 00 00 00 00 f0 bf
    

    If instead you get these bytes:

    bf f0 00 00 00 00 00 00
    

    you are encountering an endian order problem. If that’s the case, then you
    would change the line in my example code to

        i = (int)(n >> ((sizeof(n) - j - 1)*8)) & 0x0ff;
    

    That just reverses the order in which the long long is decoded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My WCF service involves the sending of a dataset (in csv format) data between
My web server has a lot of dependencies for sending back data, when it
I am writing a networked application in Java, to communicate between the client and
I need to maintain a connection between a server and multiple clients so that
I have a connection protocol that has been defined by our customer. Data are
I have been sending binary data between applications lots of times over TCP sockets
When sending data over HTTPS, I know the content is encrypted, however I hear
They both seem to be sending data to the server inside the body, so
We have a mobile Application in a very unsteady WLan Environment. Sending Data to
I'm sending some data over AJAX to a PHP file. It's about filter options.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.