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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:58:39+00:00 2026-06-14T06:58:39+00:00

I have an 8 bit byte array in Java. The byte array consists of

  • 0

I have an 8 bit byte array in Java. The byte array consists of the high and low bytes of a 16 bit number that it’s receiving from an external sensor.

For example, the byte array might be the following:

00 00 00 01 00 02 00 03
0  1  2  3  4  5  6  7

In the byte array above, each even index is a high byte of a 16 bit number and each odd index is the low byte.

The issue I am having is when the number gets larger than 127, causing the 2’s complement representation of the number to prevail. What I really wish to do is to keep the POSITIVE unsigned number. Hence I don’t care for 2’s complement. Here is what I’ve tried to do without success.

byte[] buffer = new byte[1024]; 

//SOME CODE SNIPPET THAT PUTS NUMBERS B/W 0-500 INTO THE BUFFER RANDOMLY

for(int i = 0; i < 1024; i+=2) {
    int twoByte = 0;
    short high = 0, low = 0;
    high = (short)buffer[i];
    low = (short)buffer[i+1];
    twoByte = ((high << 8) | low);
    System.out.println(twoByte);
}

I have tried to convert to a short to make the 8 bit digit unsigned, but seems java truncates the leading 0’s ad treats it as a 8 bit and keeps the negative sign. Does anyone know maybe a better method?

## EDIT: I don’t know if it matters, but it might be worth mentioning that this is an application running on Android.##

Cheers,

  • 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-14T06:58:40+00:00Added an answer on June 14, 2026 at 6:58 am

    Try:

    int high = ((int)buffer[i]) & 0xff;
    int low = ((int)buffer[i+1]) & 0xff;
    
    int twoBytes = (high << 8) | low;
    

    Sample code:

    byte[] buffer = new byte[] { (byte) 0x16, (byte) 0xfa };
    
    int high = ((int)buffer[0]) & 0xff;
    int low = ((int)buffer[1]) & 0xff;
    int twoBytes = (high << 8) | low;
    

    Values:

    buffer   = [ 22, -6 ]
    high     = 22
    low      = 250
    twoBytes = 5882 
    

    5882 is 0x16fa as hexadecimal, it works.

    Check that you correctly read the bytes from your sensor.

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

Sidebar

Related Questions

So I'm reading a 12 bit integer from a byte array. That number can
I have a 128-byte (1024-bit) modulus (in a byte array format) and my exponent
I have 1 bit in a byte (always in the lowest order position) that
I wrote a serializer (to Byte Array) for dictionaries that have a string key,
I have a byte[] array received in TCP Client.The array contains a 24-bit RGB
I have a problem with converting raw-bytes values into java types. I am receiving
I have array from serial read, named sensor_buffer . It contains 21 bytes. gyro_out_X=((sensor_buffer[1]<<8)+sensor_buffer[2]);
I have a byte array that has hex values and I initially put those
JAXB generated from XSD base64Binary and hexBinary to byte array. I have xsd element
I have a two's complement representation of a number, in a byte array, and

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.