[英]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.*. 我想在Little Endian中将2byte数组转换为Int而不使用java.nio。*。 How can I accomplish this? 我怎么能做到这一点?
With regards 带着敬意
这应该是技巧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);
因此它应该是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 你有2个字节意味着16位,因为在小印度最不重要的16位单元存储值你可以在java中使用bitvise操作
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.