简体   繁体   English

从2个字节转换整数

[英]converting integer from 2 bytes

First of all I have a couple hours experience with java, so if its a bit easy question sorry about that. 首先,我有几个小时的Java经验,因此,如果这是一个简单的问题,对不起。

Now, I have a byte array with size 2 And I want to get corresponding integer value from it. 现在,我有一个大小为2的字节数组,我想从中获取相应的整数值。 I use little endian notation. 我使用小尾数法表示法。 Here is my code: 这是我的代码:

public static int convertInt(byte[] twoByte){
       //TODO
}

Is there anyone to help me ? 有没有人可以帮助我?

return (twoByte[0] & 0xff) + ((twoByte[1] & 0xff) << 8)

给出正确的结果,将两个字节解释为无符号数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM