nurgasemetey
nurgasemetey

Reputation: 770

2 byte array in Little Endian to int without java.nio.*

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

With regards

Upvotes: 0

Views: 2217

Answers (3)

benjay
benjay

Reputation: 53

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.

Upvotes: 1

George
George

Reputation: 416

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

Upvotes: 1

Rohit Sachan
Rohit Sachan

Reputation: 1222

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

Upvotes: 0

Related Questions