Literal Syntax For byte[] arrays using Hex notation..?

后端 未结 4 1972

The compiler seems to be ok with this (single digit hex values only):

byte[] rawbytes={0xa, 0x2, 0xf};

But not this:

byte[]         


        
4条回答
  •  生来不讨喜
    2020-12-05 13:15

    byte is signed and 0xff = 255 is too big. The valid range is (-128 .. 127).

    Example code:

    public static void main(String[] args) {
        byte b = (byte) 0xff;    // = -1
        int i = b;               // = -1
        int j = b & 0xff;        // = 255
    
        System.out.printf("b=%s, i=%s, j=%s", b,i,j);
    }
    

提交回复
热议问题