Search code examples
javatcpbyteprotocols

I dont know 0xAA55 get in byte array (size 2)


I developing TCP Server Program but I stucked this Server's Protocol the header is fixed by 0xAA55, header size is 2 Byte this is the problem I dont know fill in 0xAA55 to byte array

byte[] tmp = new byte[2]; tmp = 0xAA55;

this is not work..


Solution

  • You could wrap tmp with a ByteBuffer and then use ByteBuffer.putShort(short) like

    byte[] tmp = new byte[2];
    ByteBuffer bb = ByteBuffer.wrap(tmp);
    bb.putShort((short) 0xAA55);
    System.out.println(Arrays.toString(tmp));