java - Raw data parsing -
i have byte array length 18 following structure:
unsigned int a; unsigned int b; unsigned short c; unsigned long d;
i know don't have unsigned * data type in java. after searching form web, find should use bigger data type store value. example, should use int store unsigned short variable.
i have 2 questions. first 1 how parsing byte array value array variable using. second 1 how handle unsigned long variable? know may biginteger
can me not sure how work out.
there @ least 2 practical ways decode data. either byte-fiddling manually (assuming little-endian):
a = (array[0] & 0xff) | ((array[1] & 0xff) << 8) | ((array[2] & 0xff) << 16) | ((array[3] & 0xff) << 24); // analogously others // "& 0xff"s stripping sign-extended bits.
alternatively, can use java.nio.bytebuffer
:
bytebuffer buf = bytebuffer.wrap(array); buf.order(byteorder.little_endian); = buf.getint(); // analogously others
there are, of course, infinitude of ways go it; these examples. initializing datainputstream
bytearrayinputstream
example.
as handling unsigned long
s, java can't generally, depending on kind of operations intend carry out on them, may not have care. general bit-fiddling ands, ors , bit-shifting generally, , simple arithmetic addition , subtraction, don't care signedness, after (though check out differences between >>
, >>>
).
if want general unsigned long
arithmetic, indeed biginteger
s (practical) way.
Comments
Post a Comment