byte array to unsigned int in java

后端 未结 4 1881
暗喜
暗喜 2021-01-05 04:34

I\'m converting a byte array into int by doing this:

ByteArrayInputStream bais = new ByteArrayInputStream (data);
DataInputStream dis = new Data         


        
4条回答
  •  旧时难觅i
    2021-01-05 05:36

    An int is always a signed, 32-bit number in Java. However, this only matters if you are doing math with it. If all you care about is the pattern of 0 and 1 bits, simply ignore the sign.

    If you do need to do some math, convert it to a long by masking:

    long l = j & 0xFFFFFFFFL;
    

    Do all arithmetic with long operands, modulo 0xFFFFFFFFL. When you are done, cast the result back to an int and transmit it.

提交回复
热议问题