I need to convert 2 byte array ( byte[2] ) to integer value in java. How can I do that?
-
5This question could use some clarification. As it stands now, we will only see what you mean by which answer you select. – Victor Zamanian Dec 20 '10 at 18:45
-
2Does this answer your question? [Read two bytes into an integer?](https://stackoverflow.com/questions/4768933/read-two-bytes-into-an-integer) – David Krell May 24 '23 at 11:00
6 Answers
You can use ByteBuffer
for this:
ByteBuffer buffer = ByteBuffer.wrap(myArray);
buffer.order(ByteOrder.LITTLE_ENDIAN); // if you want little-endian
int result = buffer.getShort();
See also Convert 4 bytes to int.
-
1Thank you this worked. Will ByteBuffer takes care of signed and unsigned bytes? – keshav Dec 20 '10 at 19:54
-
@keshav: what do you mean? There is no such thing as a "signed" or "unsigned" byte. A byte is just a group of `0` s and `1` s; the **only** meaning is in how you interpret those numbers. – Matt Ball Dec 20 '10 at 20:13
-
@Matt: Once the conversion is done, if the converted number is negative ( say -100 ), will I see it as -100? – keshav Dec 20 '10 at 20:33
-
@keshav: why don't you try it and see? :) Honestly - I don't know without testing it myself. – Matt Ball Dec 20 '10 at 20:36
-
1@keshav: I tested it. `ByteBuffer` will handle this correctly. That is, **iff** the bytes represent a negative number, then this code will output a negative `int`. Demos: [here](http://ideone.com/MUe5p) and [here](http://ideone.com/xwBIW). Remember, it's only negative if the most significant (leftmost) bit is `1`. – Matt Ball Dec 20 '10 at 21:19
-
1This won't work, as `+` has higher precedence than `<<`, so `a << 8 + b` is the same as `a << (8 + b)` which is not at all what you want. In addition, `byte` in java is signed, so bad things will happen if the low-order byte is negative. – Chris Dodd Dec 27 '10 at 23:52
-
This answer should be split, because the first part is wrong, while the ByteBuffer part is nice and clean, and actually works. – user1338062 Sep 18 '13 at 16:54
-
@MattBall yep, upvoted! I actually got bitten by the wrong part, although I wrote it myself :) – user1338062 Sep 18 '13 at 19:32
In Java, Bytes are signed, which means a byte's value can be negative, and when that happens, @MattBall's original solution won't work.
For example, if the binary form of the bytes array are like this:
1000 1101 1000 1101
then myArray[0] is 1000 1101
and myArray[1] is 1000 1101
, the decimal value of byte 1000 1101
is -115
instead of 141(= 2^7 + 2^3 + 2^2 + 2^0)
if we use
int result = (myArray[0] << 8) + myArray[1]
the value would be -16191
which is WRONG.
The reason why its wrong is that when we interpret a 2-byte array into integer, all the bytes are unsigned, so when translating, we should map the signed bytes to unsigned integer:
((myArray[0] & 0xff) << 8) + (myArray[1] & 0xff)
the result is 36237
, use a calculator or ByteBuffer to check if its correct(I have done it, and yes, it's correct).

- 2,158
- 3
- 27
- 50
Also, if you can use the Guava library:
Ints.fromByteArray(0, 0, myArray[1], myArray[0]);
It was worth mentioning since a lot of projects use it anyway.

- 7,070
- 2
- 40
- 43
Simply do this:
return new BigInteger(byte[] yourByteArray).intValue();
Works great on Bluetooth command conversions etc. No need to worry about signed vs. unsigned conversion.

- 3,455
- 28
- 31
-
Note there's a constructor in this class where you can manually specify you wish to handle unsigned values. – brunch875 Jun 29 '19 at 12:08
Well, each byte is an integer in the range -128..127, so you need a way to map a pair of integers to a single integer. There are many ways of doing that, depending on what you have encoded in the pair of bytes. The most common will be storing a 16-bit signed integer as a pair of bytes. Converting that back to an integer depends on whether you store it big-endian form:
(byte_array[0]<<8) + (byte_array[1] & 0xff)
or little endian:
(byte_array[1]<<8) + (byte_array[0] & 0xff)

- 119,907
- 13
- 134
- 226
import java.io.*;
public class ByteArray {
public static void main(String[] args) throws IOException {
File f=new File("c:/users/sample.txt");
byte[]b={1,2,3,4,5};
ByteArrayInputStream is=new ByteArrayInputStream(b);
int i;
while((i=is.read())!=-1) {
System.out.println((int)i);
FileOutputStream f1=new FileOutputStream(f);
FileOutputStream f2=new FileOutputStream(f);
ByteArrayOutputStream b1=new ByteArrayOutputStream();
b1.write(6545);
b1.writeTo(f1);
b1.writeTo(f2);
b1.close();
}

- 3,134
- 13
- 34
- 54