将大端转换为2字节数

时间:2014-05-02 05:59:50

标签: java byte endianness

以下是通过套接字以5字节数据发送的参数:

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

methodName = 5
payload = 2151

我想只用5个字节发送三个以上的数据。 最后的发送字节是0500020867.如何获得这个最后的字节?

  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

任何人都可以帮助我。任何形式的帮助都是值得的。 谢谢, 阿卡什

3 个答案:

答案 0 :(得分:2)

Java是already big Endian。无需为网络字节顺序执行转换。

这是一个展示这一点的示例程序。这主要是直接来自this answer。从输出中可以看出,前两个字节是0,因此首先存储MSB。

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

输出:

0
0
8
67

更新:以下是我们如何使OP更新的问题发挥作用。

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

答案 1 :(得分:2)

您已经使用的ByteBuffer类具有设置字节序,以及将不同长度的值放入缓冲区的方法。我建议你用它。

类似的东西:

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

输出:

05 00 02 08 67

正如所料。


PS:原始代码中的问题是:

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

答案 2 :(得分:1)

扔掉所有内容并使用DataOutputStream.writeInt(),或者,如您所希望的那样,使用writeShort().

请注意,您的方法SHORT_little_endian_TO_big_endian()名称错误,因为它的作用与输入的字节序无关,而该字节序只能按平台的本机顺序排列。它可以在小端和大端硬件上使用C语言。