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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:24:09+00:00 2026-06-17T11:24:09+00:00

I have an array of bytes (because unsigned byte isn’t an option) and need

  • 0

I have an array of bytes (because unsigned byte isn’t an option) and need to take 4 of them into a 32 bit int. I’m using this:

byte rdbuf[] = new byte[fileLen+1];
int i = (rdbuf[i++]) | ((rdbuf[i++]<<8)&0xff00) | ((rdbuf[i++]<<16)&0xff0000) | ((rdbuf[i++]<<24)&0xff000000);

If i don’t do all the logical ands, it sign extends the bytes which is clearly not what I want.

In c this would be a no brainer. Is there a better way in Java?

  • 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-17T11:24:10+00:00Added an answer on June 17, 2026 at 11:24 am

    You do not have to do this, you can use a ByteBuffer:

    int i = ByteBuffer.wrap(rdbuf).order(ByteOrder.LITTLE_ENDIAN).getInt();
    

    If you have many ints to read, the code becomes:

    ByteBuffer buf = ByteBuffer.wrap(rdbuf).order(ByteOrder.LITTLE_ENDIAN);
    
    while (buf.remaining() >= 4) // at least four bytes
        i = bb.getInt();
    

    Javadoc here. Recommended for use in any situation where binary data has to be dealt with (whether you read or write such data). Can do little endian, big endian and even native ordering. (NOTE: big endian by default).

    (edit: @PeterLawrey rightly mentions that this looks like little endian data, fixed code extract — also, see his answer for how to wrap the contents of a file directly into a ByteBuffer)

    NOTES:

    • ByteOrder has a static method called .nativeOrder(), which returns the byte order used by the underlying architecture;
    • a ByteBuffer has a builtin offset; the current offset can be queried using .position(), and modified using .position(int); .remaining() will return the number of bytes left to read from the current offset until the end;
    • there are relative methods which will read from/write at the buffer’s current offset, and absolute methods, which will read from/write at an offset you specify.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: byte array to Int Array I have a large array of bytes,
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
I have an array of bytes in my C# class, and need to persist
I have a byte array of around 10,000 bytes which is basically a blob
I have a byte array that may or may not have null bytes at
I have a byte that is an array of 30 bytes, but when I
I have a unsigned char array[248]; filled with bytes. Like 2F AF FF 00
I have an unsigned int number (2 byte) and I want to convert it
I have a byte stream I need parsed into a struct, and I also
I have an array of bytes and i want to determine if the contents

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.