Insert byte into byte array

I really can't believe what I am asking about this, but all I read is either converting from int to byte, or a string to byte, or something like that. I am literally trying to insert a byte into a byte array. Or, for that matter, initialize a byte array with bytes, not ints.

byte[] header = {0x8b, 0x1f, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03}; 

The compiler complains that they are ints. I am trying to insert bytes.

+4
source share
4 answers

byte is a signed integer in the range [-128,127] . 0x8b is 139d , so you need to give it in byte (byte)0x8b or use a value in the appropriate range, for example -0x75 (the equivalent of dropping 0x8b to byte ).

+9
source

Compiler threats are like 0x8b as integers, so you need to explicitly point to the byte

 byte[] header = { (byte) 0x0b, (byte) 0x1f }; 
+3
source

A byte is a signed integer, so it cannot exceed 127. 0x8b therefore too large.

Link

+3
source
 public static byte[] bytes(byte... bytes){ return bytes; } byte[] header=bytes(0x8b, 0x1f, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03); 
0
source

Source: https://habr.com/ru/post/1442646/


All Articles