Java is already big Endian. There is no need to perform the conversion for Network Byte Order.
Here is an example program demonstrating this. This is mostly taken directly from this answer. As you can see from the output, the first two bytes are 0
, so the MSB is stored first.
import java.util.*;
import java.nio.ByteBuffer;
public class Main{
public static void main(String[] args) {
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(2151);
byte[] result = b.array();
for (int i = 0; i < 4; i++)
System.out.printf("%x\n", result[i]);
}
}
Output:
0
0
8
67
Update: Here is how we make the OP's updated question work.
import java.util.*;
import java.nio.ByteBuffer;
public class Main{
public static byte[] getBytes(int input) {
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(input);
return b.array();
}
public static void main(String[] args) {
int methodname = 5;
int payload = 2151;
byte[] payloadBytes = getBytes(payload);
int payloadlength = 2; // We always assume this is 2.
byte[] result = new byte[5];
result[0] = (byte) (methodname);
// The following two lines are the same always.
result[1] = 0;
result[2] = (byte) (payloadlength);
// Here we put in the payload, ignoring the first two Most Significant Bytes
result[3] = payloadBytes[2];
result[4] = payloadBytes[3];
for (int i = 0; i < 5; i++)
System.out.printf("%x\n", result[i]);
}
}