Java hex

I have a message

static int[] message = {
        0x01, 0x10, 0x00,
        0x01, // port addres 01 - 08
        0x00, 0x01, 0x02,
        0x06, 0x00,
        0xA4, 0x21
};

I know the data is correct as I am writing it to a COM port with RXTX and I got the correct HW response

I know that it 0x01is a value 1and sent really like 01(these are two bits, a quarter byte)

When I need to configure a message, it generates values ​​like this?

message[index] = 1 & 0xff

I see the result of this fragment, and it looks right

for (int i = 0; i < 255; i++) {
    System.out.println(i & 0xff);
}

Is there any sheet you would recommend me to read?
Saves these numbers to the intright, since we cannot use byte (-128, +127)for values ​​up to <0x00, 0xFF>range

+5
source share
2 answers

, ByteBuffer , , .

, 3 1 :

public final class Message
{
    private static final MESSAGE_SIZE = 4;
    private static final PORT_OFFSET=3;

    private final ByteBuffer buf;

    public Message()
    {
        buf = ByteBuffer.allocate(MESSAGE_SIZE);
    }

    public void setPort(final byte b)
    {
        buf.put(PORT_OFFSET, b);
    }

    // etc

    public byte[] getRaw()
    {
        return buf.array();
    }
}

ByteBuffer hashCode() equals() , - get/put ( ).

/ NRPE.

+4

, 0x01 1 01 ( )

, -, . Java int - 32- (). , , , , . , , , , , , , , .

OutputStream#write, , , . OutputStream - , ; . , , . A FileOutputStream a ByteArrayOutputStream a PipedOutputStream -.

+2

All Articles