0

我需要一些关于如何在 Java / Android 中做某事的解释。我需要构造一个字节数组/数据包,以便我可以通过 http 请求发送它。我需要它看起来像这样:

- 3 bytes reserved (zero padded)
- 1 byte - Operation Group
- 1 byte - Packet type
- the rest depends on the above

但我不知道我怎么能这样构造byte[]

这是我尝试过的:

        String padding = "0000000000"; // first part of packet
        String group = "0xA"; // second part of packet
        String type = "02"; // third part of packet
        String content = "ThisIsATestStringWhichYouWillReadButItsADumbAssStringDudeSorryForYou"; // last part of packet

        String wholePacket = padding.concat(group.concat(type.concat(content)));
        Log.v("","wholePacket : "+wholePacket);

        byte[] bytes = EncodingUtils.getBytes(wholePacket, "UTF-8");

有什么建议么?

4

3 回答 3

1

您只需要创建一个byte[]大小为sizeof(rest)+ 3 + 1 + 1 的 a:

byte[] payload = new byte[] { 0xCA, 0xFE }; // use whatever you need to get your payload into bytes

byte[] buf = new byte[3 + 1 + 1 + payload.length];
// new arrays are automatically initialized with 0, so you don't need to set bytes 0-2 to 0x00
buf[3] = 0x0A; // group
buf[4] = 4; // type

// copy payload into the target
System.arraycopy(payload, 0, buf, 3 + 1 + 1, payload.length);

但是,我建议您使用 Stream 而不是 byte[] 因为无论如何您都需要通过 HTTP 连接(这已经是一个流)发送它:

byte[] payload = new byte[] { 0xCA, 0xFE };
OutputStream target = ... // get the output stream of you http connection.
byte group = 0x0a;
byte type = 4;
target.write(new byte[] { 0x00, 0x00, 0x00, group, type }, 0, 5);
target.write(payload);
target.flush();
于 2012-08-07T14:44:20.073 回答
0

使用您提供的信息就这么简单。

byte[] info = new byte[] {0,0,0, 
                          1 /* Operation Group */,
                          3 /* Packet type */, 
                          "the rest"};

或者

byte[] info = new byte[length]; 
info[0] = 0;
info[1] = 0;
info[2] = 0;
info[3] = 4; // Operation Group
info[4] = 9; // Packet type

for(int i =5; i < info.length; i++) {
   info[i] = x; //value
}
于 2012-08-07T14:28:26.837 回答
0

看起来你在正确的轨道上。由于最后一部分的长度可能不同,因此将其构建为字符串然后将该字符串转换为字节会更容易。在从 String 转换为字节数组时以及稍后(可能想要)将字节数组转换回 String 时,请注意使用相同的编码:

    byte[] byteArray;

    byte[] firstPart = new byte[]{0,0,0};
    byte secondPart = 0; //whatever your Operation Group value is
    byte thirdPart = 0; //whatever your Packet type is value is

    String lastParrtString = "blaBLAbla";
    final String encoding = "UTF8"; //establish an encoding for the string representing the last part

    byte[] lastPart = lastParrtString.getBytes(encoding);

    int byteArrayLength = firstPart.length + 1 + 1 +  lastPart.length;              
    byteArray = new byte[byteArrayLength];

    int pos = 0;
    for(/*initialized above*/;pos < firstPart.length; pos++) {
        byteArray[pos] = firstPart[pos];
    }
    byteArray[++pos] = secondPart;
    byteArray[++pos] = thirdPart;

    int tempPos = 0;
    for( ;pos < byteArray.length; pos++) {
        byteArray[pos] = lastPart[tempPos++];
    }

    System.out.println(Arrays.toString(byteArray));
    System.out.println(Arrays.toString(lastPart));
于 2012-08-07T14:50:04.777 回答