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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:14:56+00:00 2026-05-26T16:14:56+00:00

I have a byte array in Java of size 4, which I am trying

  • 0

I have a byte array in Java of size 4, which I am trying to put the first two bytes of into ByteBuffer.

Here is how I am doing it:

byte[] array = new byte[4];
ByteBuffer buff = ByteBuffer.allocate(2);
buff.put(array, 0, 2);

What’s wrong with my code?

EDIT:

byte[] currRecord = new byte[4];
byte[] leftRecord = new byte[4];
// Code that populates the records
ByteBuffer currKey = ByteBuffer.allocate(2);
currKey = currKey.put(currRecord, 0, 2);
ByteBuffer leftKey = ByteBuffer.allocate(2);
leftKey = leftKey.put(leftRecord, 0, 2);

Then I am trying to compare both ByteBuffers as follows:

if (currKey.compareTo(leftKey) >= 0)
    return;

My comparison is always wrong. When debugging, I am pretty sure currRecord and leftRecord have the right values. The ByteBuffers also have the right values (according to the debugger). What is the problem here?

  • 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-26T16:14:57+00:00Added an answer on May 26, 2026 at 4:14 pm

    The compareTo compares the remaining bytes of the buffers. Therefore you must first flip() both buffers before the comparison.

    Without the flip you will compare the bytes[2..3] in each buffer. Because you did not write those bytes, they all will be zero. With the flip you will compare the bytes[0..1] which contains the data you have written from the arrays.

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

Sidebar

Related Questions

I have a byte array which needs to be marshalled into the following struct:
I have a byte array of around 10,000 bytes which is basically a blob
Using Java, I have a class which retrieves a webpage as a byte array.
I have a byte[] in Java which reports its length as 256 bytes which
I have a byte array, received from java application via network. I need to
I have a byte array highlighted below, how do I insert it into a
I have a byte array that may or may not have null bytes at
I have a byte array of size 4 byte[] source = new byte[4]; Now
How to Convert a byte array into an int array? I have a byte
I have a fragment of bytes in a byte[]. The total size of the

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.