如何在java中读取大于127的字节?

时间:2021-07-07 22:52:28

alright, so my code to read bytes into a int is like so:

我把字节读入int的代码是这样的

int offset = (byte << 16) | (byte2  << 8) | byte3;

And it's reading the bytes "00 00 be" as -66.

读取字节为-66。

How do I read it as the 190 it's meant to be?

我怎么把它读成它应该是的190呢?

2 个解决方案

#1


5  

byte b = -66;
int i = b & 0xff;

#2


1  

    byte b = -66;
    int i = b < 0 ? b + 256 : b;

It might be useful declare helper function for this.

声明helper函数可能是有用的。

#1


5  

byte b = -66;
int i = b & 0xff;

#2


1  

    byte b = -66;
    int i = b < 0 ? b + 256 : b;

It might be useful declare helper function for this.

声明helper函数可能是有用的。