0

I need a little explanation of how can I do something in Java / Android. I need to construct a byte array / data packet so I can send it via http request. I need it to look like this :

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

But I don't know how can I construct byte[] like this.

Here is what I've tried :

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

Any suggestions?

5
  • 1. What is "the rest" - how long can it be? 2. Show us some code, what have you tried? Commented Aug 7, 2012 at 14:21
  • What's the actual issue? You create a byte array by putting bytes into an array. Commented Aug 7, 2012 at 14:23
  • the rest of it is dynamic, and i've just update my question with the code which I've tried. Commented Aug 7, 2012 at 14:25
  • Why not just use bytes, especially for the byte-ish stuff? You're converting strings into byte representation of those strings :( Commented Aug 7, 2012 at 14:28
  • Your padding is 10 bytes length, how does this cope with the spec? (3 bytes) And what is wrong with what you wrote - I don't see a question here. Commented Aug 7, 2012 at 14:29

3 Answers 3

1

You only need to create a byte[] with a size of 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);

However, I would suggest that you use a Stream instead of a byte[] since you need to send it through an HTTP connection (which is already a stream) anyway:

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();
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you're on the right track there. Since the last part can vary in lenght, it's easier to build it as a String and then convert that string to bytes. Just be careful to use the same encoding when going from String to byte array and later when you (might want to) convert the byte array back to 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));

Comments

0

Simple as this with information you've provided.

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
}

1 Comment

That won't even compile... You cannot pass strings as a values to byte array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.