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 8601327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:48:59+00:00 2026-06-12T01:48:59+00:00

I have a short counter which increments and is used as an identifier. I

  • 0

I have a short counter which increments and is used as an identifier. I need to send the identifier as binary data on a network socket.

So if I have a short value I can do this to ‘pack’ in two bytes:

short id = 1;
byte val = (byte)(id & 0xFF);
byte val2 = (byte)((id >>> 8) & 0xFF);
System.out.printf("id=%d, val = id & 0xFF = %d, val2 = (id >>> 8) & 0xFF = %d\n", 
                   id, val, val2);

Now when id = 1 this works fine.

But when id = 255 I get:

short id = 255;
byte val = (byte)(id & 0xFF);           //val = -1
byte val2 = (byte)((id >>> 8) & 0xFF);  //val2 = 0

Now I know that 255 in binary is 11111111 and this means -1 for a java byte value. Am I correct in thinking that how java interprets it is immaterial and if I send as byte binary 11111111 then at the other end it can interpret this however it wants?

  • 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-06-12T01:49:00+00:00Added an answer on June 12, 2026 at 1:49 am

    Yes, your thinkings are right.

    Just for your information: there is a very nice class that does this for you. It is called DataInputStream and DataOutputStream.

    Socket socket = ...;
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    
    /* Write a short */
    short out = 255;
    dos.writeShort(out);
    
    /* Read a short */
    short in = dis.readShort();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a short script where i'm trying to grab data from a URL
I have a short loop I used to create several .csv files. The loop
I have to make a program which has a shared variable (counter with initial
I have an android app which connects to a server using a socket connection
So i have some bodies(which i can create more and more at runtime), and
I have a problem which I can't find a solution to. I have an
I have a counter hash that I am trying to sort by count. The
I am coding a messaging system and I don't want to have short IDs
I have a short access/mySQL question. I have a mapping table on the format
I have a short question i have wrote this in java. Old code: class

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.