4

I really can't believe I'm asking this but everything I read is either converting from int to byte to string to byte or something. 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'm trying to insert bytes.

4 Answers 4

9

byte is a signed integer in the range [-128,127]. 0x8b is 139d, so you'll need to cast it to a byte (byte)0x8b or use a value in the proper range such as -0x75 (the equivalent of casting 0x8b to byte).

                                                                                        

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I had a feeling it was something very simple. I feel kind of bad for even having to ask.
don't feel bad, it's not obvious at all. other languages (like C) will accept similar code without even warning sometimes...
3

The compiler threats literals like 0x8b as integers, so you have to explicitly cast to byte

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

Comments

3

Bytes are signed integers, so cannot exceed 127. 0x8b is therefore too big.

Reference

Comments

0
public static byte[] bytes(byte... bytes){ return bytes; }

byte[] header=bytes(0x8b, 0x1f, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.