tags:

views:

324

answers:

7

Thanks for all above answers but didn't really help me well just to give an idea what exactlty i wants to do: I am coding for some sort of packet which has different fields with differnt length in bytes

so field1 is 2 byte long field2 is 3 bytes long field3 is 6 bytes long and when i addup these fields, i should be getting 11 bytes in length...

But i have no idea how can i declare something with this byte long...

Please help...thanks in advance

+5  A: 

Use an array:

byte[] byteArray = new byte[11];
Steve McLeod
I already knew about the byte primitive, but thinking about it now, I wonder if we could use it to implement a uint class...
PiPeep
+2  A: 

How's about:

byte[] arr = new byte[11];
Yuval A
A: 

I have found that java.nio.ByteBuffer is typically better for this sort of thing. It has nice methods for dealing with interpreting the bytes in the buffer. The docs are here.

import java.nio.ByteBuffer;

ByteBuffer buffer = ByteBuffer.allocate(11);

Check out the docs and look at the nice methods such as getInt() and getChar().

Tom
Also, before you go making your own protocol and stuff to parse and interpret it, you may want to check something out like Google's Protocol Buffers: http://code.google.com/apis/protocolbuffers/docs/overview.html. They sound like they could get the job done and the code for handling the protocol messages could be less cumbersome. It does add more dependencies in your code and such (there is a protocol buffer compiler you would need to invoke), but I find them easy to use :-).
Tom
A: 

Java has a limited collection of primitive types, which all have a fixed size. You can see a list of them here. That means you can't decide how many bytes your variable will consist of.

Of course, as others have already mentioned, you can always create a new byte[11]. Note that Java's byte is signed, however. It goes from -128 to 127, not from 0 to 255.

jqno
+1  A: 

You could use a class to represent your packet:

public class Packet
{
   public byte[] Field1, Field2, Field3;

   public Packet(byte[] packetBytes) 
   {
      ByteBuffer packet = ByteBuffer.wrap(packetBytes);
      Field1 = new byte[2];
      Field2 = new byte[3];
      Field3 = new byte[6];
      packet.get(Field1, 0, 2);
      packet.get(Field2, 2, 3);
      packet.get(Field3, 5, 6);
   }
}

ByteBuffer is good for byte-manipulation.

Jeff Meatball Yang
First... did I miss something... "struct" in java?? Also, isn't manipulting the actual ByteBuffer better, and creating a wrapper class around it? Instead of using get() and filling byte arrays, you could use packet.getInt(); packet.getChar(); etc etc for whatever types are nested in the packet.
Tom
whoops - I started answering in C# - then switched to Java.
Jeff Meatball Yang
A: 

I recommend the utility classes in Javolution for dealing with binary protocol streams such as this. They've come in handy for me several times when dealing with low-level binary streams.

jsight
A: 

You should probably design your code to separate the message you want to manipulate in java from the wire level format you need to read/write.

e.g. If you have a ScreenResolution concept, you could represent it in java with a ScreenResolution class:

public class ScreenResolution {
   public int height;
   public int width;
}

This class is easy to work with in Java. Transforming this to a packet that can be transmitted over a network/saved to a file, etc. according to some file format or protocol is another concern. Say the height and width is to be laid out in 3 bytes each, with some ID and length for the "wire format", you make something like

public byte[] marshalScreenResolution(ScreenResolution obj) {
   byte[] buf = new byte[9];
 //length of this packet, 2 bytes
   buf[0] = 0;
   buf[1] = 9;
   buf[2] = SCREENRESOLUTION_OPCODE;
   //marshal the height/width , 3 least significant bytes.
   buf[3] = (obj.height&0xff0000) >> 16;
   buf[4] = (obj.height&0x00ff00) >> 8;
   buf[5] = (obj.height&0x0000ff) ;
   buf[6] = (obj.width&0xff0000) >> 16;
   buf[7] = (obj.width&0x00ff00) >> 8;
   buf[8] = (obj.width&0x0000ff) ;

   return buf;
}

And you make a demarshalScreenResolution function for going from a packet to a ScreenResolution object. The point is you decouple the representation in java from the external representation, and you assemble the fields in the external representation using bytes + some basic bit fiddling.

nos