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

后端 未结 4 1974

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:24

    As the other answered already said, byte is a signed type in Java. The range is from -128 to 127 inclusive. So 0xff is equal to -0x01. You can use 0xff instead of -0x01 if you add a manual cast:

    byte[] rawbytes={0xa, 0x2, (byte) 0xff};
    

提交回复
热议问题