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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:19:25+00:00 2026-05-25T02:19:25+00:00

I have a spec which reads the next two bytes are signed int. To

  • 0

I have a spec which reads the next two bytes are signed int.

To read that in java i have the following

When i read a signed int in java using the following code i get a value of 65449

Logic for calculation of unsigned

int a =(byte[1] & 0xff) <<8

int b =(byte[0] & 0xff) <<0

int c = a+b 

I believe this is wrong because if i and with 0xff i get an unsigned equivalent

so i removed the & 0xff and the logic as given below

int a = byte[1] <<8
int b = byte[0] << 0
int c = a+b
which gives me the value -343
byte[1] =-1
byte[0]=-87

I tried to offset these values with the way the spec reads but this looks wrong.Since the size of the heap doesnt fall under this.

Which is the right way to do for signed int calculation in java?

Here is how the spec goes

somespec() { xtype 8 uint8 xStyle 16 int16 }
xStyle :A signed integer that represents an offset (in bytes) from the start of this Widget() structure to the start of an xStyle() structure that expresses inherited styles for defined by page widget as well as styles that apply specifically to this widget.

  • 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-25T02:19:26+00:00Added an answer on May 25, 2026 at 2:19 am

    If you value is a signed 16-bit you want a short and int is 32-bit which can also hold the same values but not so naturally.

    It appears you wants a signed little endian 16-bit value.

    byte[] bytes = 
    short s = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getShort();
    

    or

    short s = (short) ((bytes[0] & 0xff) | (bytes[1] << 8));
    

    BTW: You can use an int but its not so simple.

    // to get a sign extension.
    int i = ((bytes[0] & 0xff) | (bytes[1] << 8)) << 16 >> 16;
    

    or

    int i = (bytes[0] & 0xff) | (short) (bytes[1] << 8));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edge case. I'm building code that reads binary datafiles produced by
I have a spec in my current project that requires us to advise the
I have almost the worst spec in the world. An application that exists and
In a branch spec, I have the following view: //depot/dev/t/a/g/... //depot/dev/t/r/g/... -//depot/dev/t/a/g/p/o*/... //depot/dev/t/r/g/p/... Perforce
Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I am trying to read a binary file and parse the bytes I have
I have a jQuery script that will display specific divs depending on which select
Hi I am using Fedora 10 linux. I have created a .spec file now

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.