It is necessary to set a constant signature array. I'm doing this now

byte sgn[] = {(byte)0xFF, (byte)0xD8, (byte)0xFF}; 

Question: Is it possible to somehow ask him not casting each element to the byte ?

Something like that

 byte [] sgn = Array.fromRaw(0xFF, 0xD8, 0xFF) 
  • In Java, there are no unsigned types, and your (byte)0xFF will turn into -1 , the same without a cast can be obtained by writing -0x01 , or just -1 as a decimal number. - Vartlok
  • @Vartlok I know that there are no unsigned types. That's why I'm forced to cast - Anton Shchyrov
  • Do you understand that 0xFF is 255? and that byte range from -128 to 127? You are trying to shove a number where it does not fit. - Vartlok
  • @AntonShchyrov is still so: why do not you want to write this array as byte sgn[] = { -1, -40, -1 }; ? - Regent
  • one
    @AntonShchyrov in this case, it remains only to write a method that the int array, specified in the form new int[] { 0xFF, 0xD8, 0xFF } , will be converted into a byte array in a loop. - Regent

1 answer 1

There is no easy way, only if you write your function.

 private byte[] bytes(int... ints) { byte b[] = new byte[ints.length]; for (int i = 0; i < ints.length; i++) { b[i] = (byte) ints[i]; } return b; }