2

I'm developing an app that receives a Datagram package with a byte array and I need to get the info on it and turn it to an object.

I've read the documentation and it says the position on the byte array for each information is stored, but I can't seem to parse it correctly to an object. Has anyone done some similar work lately?

dda
  • 6,030
  • 2
  • 25
  • 34
B. TIger
  • 459
  • 3
  • 10
  • 27

4 Answers4

5

I recommend you look into ByteBuffer. It allows you to easily wrap a byte array and extract whatever you need:

byte[] arr = ...;
ByteBuffer buff = ByteBuffer.wrap(arr);
int i = buff.getInt(); // interprets the next 4 bytes into an int
Tudor
  • 61,523
  • 12
  • 102
  • 142
1

This is something that is frequently done.

If you have the positions, you have byte arrays for each part.

So you can use the constructor of String taking bytes and a charset as args

and to construct an int, simply do something like this :

int l = 0;
for(int i =0; i < 4; i++){      
   l <<= 8;  
   l ^= (long)b[i] & 0xFF;      
} 

(assuming you received the int in Big Endian)

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

You didn't provide much information but I suggest you take a look on the link below:

How to convert a byte array to its numeric value (Java)?

For string you should be able to create a new string passing the byte array in the constructor.

Community
  • 1
  • 1
tbraun
  • 2,636
  • 31
  • 26
  • I've alredy done that but I don't get the result expected on the documentation that the tracking company sent me – B. TIger Jul 10 '12 at 17:10
0

If you are sending an Object and want to receive the Object on the other end, just processing using an ObjectOutputStream and ObjectInputStream. That way it holds it's formatting.

int I;
ObjectOutputStream.write(I)

int j;
J = (int)ObjectInputStream.read()
Matt Westlake
  • 3,499
  • 7
  • 39
  • 80
  • @fvu really? i was under the assumption that as long as both client & server had the same object that it didn't matter what language the client & server were in. I don't know what the C++ version of objectOutputStream is, but I was pretty sure that C++ and Java could communicate together. Although you are right, using a byte stream is the most "universal" way to do it, but so much easier to write using object streams. – Matt Westlake Jul 10 '12 at 16:07
  • @dystroy he states in the question that he "receives a Datagram package with a byte array and I need to get the info on it and turn it to an object". I was giving him a way to do it without writing all the code and using the memory for the buffers. – Matt Westlake Jul 10 '12 at 16:09
  • @Matt Westlake: It depends on the serialization method used. In this case it's binary serialization and I know for a fact that it's not compatible with .NET for example. – Tudor Jul 10 '12 at 16:09
  • @Tudor So if you use Object serialization (serialize the class you are using for your object passing) this can work between the higher level languages right? – Matt Westlake Jul 10 '12 at 16:11
  • @Matt Westlake: What do you mean by Object serialization? `ObjectOutputStream`? That's the one I'm talking about when I said it's binary serialization. It's not compatible except with another java program using the same mechanism. – Tudor Jul 10 '12 at 16:13
  • @MattWestlake you're using raw binary builtin Java serialization here and I think that one's only guaranteed to be compatible within a rather narrow band of Java versions. If you need something crossplatform you have to look into portable serialisation formats like Google Protocol Buffers, but these come with their own API's. – fvu Jul 10 '12 at 16:14