0

I want to convert 2byte array in Little Endian to Int without using java.nio.*. How can I accomplish this?

With regards

nurgasemetey
  • 752
  • 3
  • 15
  • 39

3 Answers3

1

This should do the trick int val = (anArray[1] & 0xff) << 8 + (anArray[0] & 0xff);

George
  • 416
  • 3
  • 11
1

Just came across this post and realised that the accepted answer will not work correctly because + has a higher precedence than <<.

Therefore it should be int val = ((anArray[1] & 0xff) << 8) + (anArray[0] & 0xff); instead.

benjay
  • 53
  • 5
0

you have 2 Byte means 16 bit because in Little indian The least significant 16-bit unit stores the value you can use bitvise operations in java

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Rohit Sachan
  • 1,178
  • 1
  • 8
  • 16