Adam Scheller wrote:You can construct an integer from variable-length bytes with BigInteger, with this constructor. First parameter is a sign, the second parameter is byte array.
Paul Clapham wrote:I think we would have to understand what "variable length integers stored in 4 bytes" means. (Storing variable-length integers in a fixed-length container already gives me cognitive dissonance.) I followed the link you posted and it didn't use the word "variable" anywhere, so it didn't help me. It seemed to say that there were integer values in the data, and it seemed to be referring to C++ coding so probably those are 32-bit integers.
You might want to look at DataInputStream for reading this kind of file; there are a few things to watch out for though. First of all what those C guys refer to as a String is actually an array of bytes to a Java programmer. And there's the little-endian versus big-endian way of storing integers, although as far as I know most modern computers all use the same endian-ness these days.
Your treatment of the VarInt32 is still not quite right. It is a variable length integer, so it can be stored in anywhere between 1 and 4 bytes depending on how big the integer actually is. If the highest bit (The & 0x80) is set, then that indicates that there is another byte that follows. If this byte isn't set then it is the end of the integer...snip
Adam Scheller wrote:I got a little lost too. Christoffer, maybe you just need to extract an integer stored in 4 bytes, not a variable-length value?
Norm Radder wrote:Can you post a sample: the contents of the 4 bytes and the correct int value that should be derived from them?
Paul Clapham wrote:Those examples look like little-endian integers to me. (Notice that there's always a zero at the end, so that's most likely the most significant byte.)
So I would suggest using readInt() from a DataInputStream and getting an integer from those 4 bytes. (Looks like you might have done that already.) Then use the Integer.reverseBytes() method to convert from little-endian to big-endian and see if the numbers you get look like what you're expecting.
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |