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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:52:07+00:00 2026-06-07T11:52:07+00:00

I am aware -that in Java- int is 4 bytes. But I wish to

  • 0

I am aware -that in Java- int is 4 bytes. But I wish to covert an int to n-bytes array, where n can be 1, 2, 3, or 4 bytes. I want to have it as signed byte/bytes, so that if I need to convert them back to int (event if it was 1 byte), I get the same signed int. I am fully aware about the possibility of precision loss when converting from int to 3 or lower bytes.

I managed to convert from int to n-byte, but converting it back for a negative number yield unsigned results.

Edit:

int to bytes (parameter n specify the number of bytes required 1,2,3, or 4 regardless of possible precession loss)

public static byte[] intToBytes(int x, int n) {
    byte[] bytes = new byte[n];
    for (int i = 0; i < n; i++, x >>>= 8)
        bytes[i] = (byte) (x & 0xFF);
    return bytes;
}

bytes to int (regardless of how many bytes 1,2,3, or 4)

public static int bytesToInt(byte[] x) {
    int value = 0;
    for(int i = 0; i < x.length; i++)
        value += ((long) x[i] & 0xffL) << (8 * i);
    return value;
}

There is probably a problem in the bytes to int converter.

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

    BigInteger.toByteArray() will do this for you …

    Returns a byte array containing the two’s-complement representation of this BigInteger. The byte array will be in big-endian byte-order: the most significant byte is in the zeroth element. The array will contain the minimum number of bytes required to represent this BigInteger, including at least one sign bit, which is (ceil((this.bitLength() + 1)/8)). (This representation is compatible with the (byte[]) constructor.)

    Example code:

    final BigInteger bi = BigInteger.valueOf(256);
    final byte[] bytes = bi.toByteArray();
    
    System.out.println(Arrays.toString(bytes));
    

    Prints:

    [1, 0]
    

    To go from the byte array back to a int, use the BigInteger(byte[]) constructor:

    final int i = new BigInteger(bytes).intValue();
    System.out.println(i);
    

    which prints the expected:

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

Sidebar

Related Questions

I am aware that Java doesn't support multiple inheritance. But if I have to
I am aware that Java includes very advanced image processing API's but what I'm
I aware that this will be a less programming question, but still... How can
I am aware that I can set null like string val = null; But
Now I am aware that I probably need to use a ListBox and can
I am aware that by default Java does not have the so-called eval (what
I am aware that you can lock an object in c# using lock but
I am aware that the purpose of volatile variables in Java is that writes
I'm aware that this is possible with the os module's os.system(color) function, but this
I am aware that MD5 hashes are not advisable for security any more but

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.