Question

Below are the parameters to send via socket in 5 byte data:

Parameters : methodname(1 byte), payloadlength(2 byte), payload(2 byte)

methodName = 5
payload = 2151

i want to send above three data in only 5 byte. The final sending byte are 0500020867. how to get this final bytes ?

  int methodname = 5;
  int payload  = 2151;

  ByteBuffer b = ByteBuffer.allocate(4);
        b.putInt(payload);
        byte[] payloadData = b.array();
        int payloadlength = payloadData.length;

    byte[] result = new byte[5];
    result[0] = (byte) (methodname);
    result[1] = (byte) (payloadlength >> 8);
    result[2] = (byte) (payloadlength);
    result[3] = (byte) (payload >> 8);
    result[4] = (byte) (payload);
    for (int i = 0; i < 5; i++)
             System.out.printf("%x\n", result[i]);
    result: 5 0 4 8 67 
    //expected result: 05 00 02 08 67

Can anybody help me out. Any sort of help would be appreciable. Thanks, Akash

Was it helpful?

Solution

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]);
    }
}

OTHER TIPS

The ByteBuffer class that you are already using has methods for setting endianness, and putting/getting values of different lengths to the buffer. I suggest you use that.

Something like:

int methodname = 5;
int payload  = 2151;
int payloadLength = 2;

ByteBuffer buffer = ByteBuffer.allocate(3 + payloadLength); // 3 = for method name + length
buffer.order(ByteOrder.BIG_ENDIAN); // Just to be explicit

buffer.put((byte) methodname);
buffer.putShort((short) payloadLength);
buffer.putShort((short) payload);

buffer.rewind();
byte[] result = new byte[buffer.capacity()]; // Could also use result = buffer.array();
buffer.get(result);

for (int i = 0; i < result.length; i++) {
    System.out.printf("%02x ", result[i]);
}

Outputs:

05 00 02 08 67

As expected.


PS: The problem in your original code is:

ByteBuffer b = ByteBuffer.allocate(4);  // Here you allocate buffer of size 4
// ...
byte[] payloadData = b.array();         // The array will always have size of buffer
int payloadlength = payloadData.length; // ...meaning pD.length will always be 4

Throw it all away and use DataOutputStream.writeInt(), or, as you want 2 bytes, writeShort().

NB your method SHORT_little_endian_TO_big_endian() is misnamed, because what it does has nothing to do with the endian-ness of the input, which has only to be in native order for the platform. It would work in C on both little-endian and big-endian hardware.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top