سؤال

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

With regards

هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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.

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top