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

+ 3 + 1+1byte[]のサイズで作成する必要があるだけです。sizeof(rest)

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

ただし、とにかくHTTP接続(すでにストリーム)を介して送信する必要があるため、byte[]の代わりにStreamを使用することをお勧めします。

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

あなたは正しい方向に進んでいるようです。最後の部分は長さが異なる可能性があるため、文字列として作成し、その文字列をバイトに変換する方が簡単です。文字列からバイト配列に移動するとき、および後でバイト配列を文字列に戻すときに(必要になる可能性がある)、同じエンコーディングを使用するように注意してください。

    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 に答える