Java与无符号数

/ Java / 没有评论 / 126浏览

Java的基本类型都是有符号数,无符号数可以用更大类型的基本类型模拟:

public class UnsignedTest {

    public static void main(String[] args) {
        byte[] bytes;
        bytes = HexUtil.hexStringToBytes("FF");
        byte signedByte = bytes[0];
        System.out.println(signedByte);
        short unsignedByte = (short) (bytes[0] & 0x00ff);
        System.out.println(unsignedByte);
    }

}

输出:

-1
255

Process finished with exit code 0