Convert unsigned byte to signed byte

前端 未结 5 1838
抹茶落季
抹茶落季 2020-12-17 21:37

Is there an easy and elegant way to convert an unsigned byte value to a signed byte value in java? For example, if all I have is the int value 240 (in binary (24 bits + 1111

5条回答
  •  醉梦人生
    2020-12-17 21:59

    Java does not have unsigned values, except for char. Consider this snippet:

    byte val = (byte)255;
    System.out.println(String.valueOf(val));
    

    The result will be -1, because the lowest 8 bits got copied over to the byte variable.

提交回复
热议问题