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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:20:35+00:00 2026-06-09T22:20:35+00:00

I’m trying to convert -101 to a byte array and then convert the byte

  • 0

I’m trying to convert -101 to a byte array and then convert the byte array back to -101. My methods below work for positive values but not for negative values. Can you suggest what I’m doing wrong? Instead of -101, the byteArrayToInt method returns 65435. Thanks!

/**
 * Converts a <code>byte</code> array to a 32-bit <code>int</code>.
 * 
 * @param array The <code>byte</code> array to convert.
 * @return The 32-bit <code>int</code> value.
 */
public static int byteArrayToInt(byte[] array) {
  ValidationUtils.checkNull(array);
  int value = 0;

  for (int i = 0; i < array.length; i++) {
    int shift = (array.length - 1 - i) * 8;
    value = value | (array[i] & 0xFF) << shift;
  }

  return value;
}

/**
 * Converts a 32-bit <code>int</code> to a <code>byte</code> array.
 * 
 * @param value The 32-bit <code>int</code> to convert.
 * @return The <code>byte</code> array.
 */
public static byte[] intToByteArray(int value, int size) {
  byte[] bytes = new byte[size];
  for (int index = 0; index < bytes.length; index++) {
    bytes[index] = (byte) (value >>> (8 * (size - index - 1)));
  }
  return bytes;
}

/**
 * Tests the utility methods in this class.
 * 
 * @param args None.
 */
public static void main(String... args) {
  System.out.println(byteArrayToInt(intToByteArray(32, 2)) == 32); // true
  System.out.println(byteArrayToInt(intToByteArray(64, 4)) == 64); // true
  System.out.println(byteArrayToInt(intToByteArray(-101, 2)) == -101); // false
  System.out.println(byteArrayToInt(intToByteArray(-101, 4)) == -101); // true
}
  • 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-09T22:20:37+00:00Added an answer on June 9, 2026 at 10:20 pm

    You need to sign-extend your number. If you haven’t already, you should read up on two’s complement representation for signed binary numbers.

    The number -101 as a 32-bit integer is 0xFFFFFF9B in hex. You convert it to a byte array of 2 bytes. That leaves just 0xFF9B. Now, when you convert it back you convert it into a 32-bit integer and the result is 0x0000FF9B, or 65435 in decimal.

    You should check the highest-order bit in your byte array and sign extend based on that. A simple way to do it would be to start with value=-1 if the highest-order bit is set and default to value=0 if it isn’t.

    Edit: An easy way to check the highest-order bit is to check if the high-order-byte is negative.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.