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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:32:48+00:00 2026-06-13T13:32:48+00:00

I want to convert bytes to int in Java. I want to assume bytes

  • 0
  1. I want to convert bytes to int in Java.
    I want to assume bytes to be unsigned bytes.
    Suppose if

    byte a = (byte)0xFF;
    
    int r = (some operation on byte a);
    

    r should be 255 not -1 in decimal.

  2. Then I want to create int value from 3 bytes.
    Suppose if

    byte b1 = (byte)0x0F;
    
    byte b2 = (byte)0xFF;
    
    byte b3 = (byte)0xFF;
    
    int r = (some operation in bytes b1, b2 and b3);
    

    Then r should be 0x000FFFFF. Byte b1 will be placed at higher 3rd position and byte b3 will be placed at lower 1st position in int value. Also my b1 will range from 0x00 to 0x0F and other bytes will be from 0x00 to 0xFF, assuming unsigned nature of bytes. If byte b1 is greater than 0x0F, I will extract only lowermost 4 bits. In short I want to extract int from 3 bytes but using only 20 bits of 3 bytes. (total 16 bits from b2 and b3, and 4 lowermost bits from b1). int r must be positive as we are creating from 3 bytes and assuming unsigned nature of bytes.

  • 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-13T13:32:49+00:00Added an answer on June 13, 2026 at 1:32 pm

    You have to be careful with the sign-extension here – unfortunately, bytes are signed in Java (as far as I know, this has caused nothing but grief).

    So you have to do a bit of masking.

    int r = (b3 & 0xFF) | ((b2 & 0xFF) << 8) | ((b1 & 0x0F) << 16);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an unsigned int number (2 byte) and I want to convert it
I want to convert an int to a byte[2] array using BCD. The int
Basically, what I want is to convert a string like 123456 to unsigned byte
I want convert an int into 2 bytes representing that int. Probably must use
I want to convert a some code which is in Java to C#. Java
I want to convert an integer ( int or long ) a big-endian byte
I'm reading from a binary file and want to convert the bytes to US
I want to convert a C-style string into a byte-vector. A working solution would
I want to convert a pointer *int to its real value int , in
I want to convert XML into binary data in Java? What is the fastest

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.