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

  • Home
  • SEARCH
  • 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 6720483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:13:03+00:00 2026-05-26T09:13:03+00:00

I have problem writing an unsigned 4 bytes int in java. Either writing a

  • 0

I have problem writing an unsigned 4 bytes int in java.

Either writing a long value in java has different result on 64 bit MacOS and 32 bit Linux (Ubuntu)
OR
Writing to network a 4 byte unsigned int has a problem.

The following call works perfectly on my local OSX

writeUInt32(999999,outputstream)

Reading it back gives me 999999

However when the application is deployed to a network writing a long value results in some other random number (I assume the endian has been switched?) and reading it gives me some other large number.

———- The complete method stack is as below—————

public void writeUInt32(long uint32,DataOutputStream stream) throws IOException {
        writeUInt16((int) (uint32 & 0xffff0000) >> 16,stream);
        writeUInt16((int) uint32 & 0x0000ffff,stream);
    }

public void writeUInt16(int uint16,DataOutputStream stream) throws IOException {
        writeUInt8(uint16 >> 8, stream);
        writeUInt8(uint16, stream);
    }

public void writeUInt8(int uint8,DataOutputStream stream) throws IOException {
        stream.write(uint8 & 0xFF);
    }

Edit: To add to the confusion writing to a file and then transporting it over the network sends me the correct value! So when outputstream points to a local file then it writes the correct values but when outputstream points to a ByteArrayOutputStream then the long value written is wrong.

  • 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-26T09:13:04+00:00Added an answer on May 26, 2026 at 9:13 am

    Just use DataOutput/InputStream.

    To write, cast your long to int

    public void writeUInt32(
          long uint32,
          DataOutputStream stream
        ) throws IOException
    {
        stream.writeInt( (int) uint32 );
    }
    

    On read, use readInt, assign to long and mask top 32 bits to get unsigned value.

    public long readUInt32(
          DataInputStream stream
        ) throws IOException
    {
        long retVal = stream.readInt( );
    
        return retVal & 0x00000000FFFFFFFFL;
    }
    

    EDIT

    From your questions, looks like you are confused about Java cast conversions and promotions for primitive types.

    Read this section of Java Spec on Conversions and Promotions: http://java.sun.com/docs/books/jls/third_edition/html/conversions.html

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

Sidebar

Related Questions

I have a problem with writing a catch clause for an exception that is
I have problem in some JavaScript that I am writing where the Switch statement
I have problem concerning python packages and testing. I'm writing an application using wx
I'm writing an app in Visual Studio C++ and I have problem with assigning
I have come across an annoying problem while writing some PHP4 code. I renamed
I have a problem that output string must be utf8-formatted, I am writing currently
I have a problem, which is not easily described. I'm writing a web application
Folks, I have a problem. I am a writing a script in python which
I have a strange problem with the validation I am writing on a form.
I am writing a class. I have encountered the problem in the title. Here

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.