Saturday, 15 January 2011

java - Raw data parsing -


I have a byte array that has length 18, with the following structure:

  unsigned int a; Unsigned int b; Unsigned minor; Unsigned long D;  

I know that I do not have unsigned * data types in Java. After searching the web form, I think I should use the larger data type to store the value. For example, I should use int to store an unsigned short variable.

I have two questions first one is how to parse the byte array, I am using the value from the array to use the variable. Second, how can handle unsigned long variables? I know that BigInteger might help me but I'm not sure how to get it done.

There are at least two practical ways to decode data. Either you can manually byte-fielding this way (assuming a little-endian):

  a = (array [0] & amp; 0xff). ((Array [1] & 0xff)  

Alternatively, you can use a java .nio.ByteBuffer :

  ByteBuffer buf = ByteBuffer.wrap (array); Buf.order (ByteOrder.LITTLE_ENDIN); A = buf.getInt (); // Informally for others  

Of course, there is an inanimate method to go about it; These were just examples. Starting a ByteArrayInputStream to DataInputStream may be another example.

Based on this, you may not have to worry. Normal bits like Ors, Ors and generally a little change, and also like simple arithmetic like addition and subtraction are negligible, do not care about signness, after all (though do check the differences between Gt; & gt; and & gt; & gt; & gt; ).

If you really want to make the normal unsigned long arithmetic, then in reality BigInteger is probably your only (practical) way.


No comments:

Post a Comment