Android如何构造类似于此的字节数组

时间:2012-08-07 14:18:28

标签: java android bytearray

我需要一些解释,如何在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");

有什么建议吗?

3 个答案:

答案 0 :(得分:1)

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

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();

答案 1 :(得分:0)

与您提供的信息一样简单。

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

OR

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
}

答案 2 :(得分:0)

看起来你在那里正确的轨道。由于最后一部分的长度可能不同,因此更容易将其构建为String,然后将该字符串转换为字节。从String到byte数组时要小心使用相同的编码,稍后当你(可能想)将字节数组转换回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));