java reading 2 byte integer in 2's complement from binary file into integer

I am trying to parse a binary file containing values. In the specs it says that each value is represented as a 2-byte integer in 2's complement format. I am reading the file into a byte array called 'data':

int i = (data[i] & 0xff) | (short) (data[i+1] << 8);

The values look fine however when I try to write them back into the file:

byte a = (byte)((tempInt >> 8) & 0xff);
byte b = (byte)(tempInt & 0xff);

'tempInt' being the value in int.

When I read in the file that I wrote, not all of the values are the same. Some of them but not all.

Am I missing something here?

UPDATE: I plottet the values for comparison. The top half are the values that i read from the file. The bottom half are the values that i read from the file, then wrote to a new file and then read again. plot I think that the graphs look too similar for it to be completely wrong.

UPDATE #2: the bytes i read from the input file:

0, 0, 59, 36, 40, 36, 23, 36, 54, 36, 54, 36, 41, 36, 46, 36, 50, 36, 52, 36, 79, 36, 79, 36, 66, 36, 65, 36, 58, 36, 58, 36, 58, 36, 45, 36, 51, 36, 65, 36, 65, 36, 76, 36, 78, 36, 69, 36, 54, 36, 68, 36, 86, 36, 85, 36, 81, 36, 81, 36, 80, 36, 84, 36, 84, 36, 80, 36, 82, 36, 85, 36, 81, 36, 80, 36, 75, 36, 75, 36, 79, 36, 91, 36, 83, 36, 64, 36, 71, 36, 79, 36, 56, 36, 38, 36, 47, 36, 51, 36, 41, 36, 48, 36, 63, 36, 56, 36, 50, 36, 57, 36, 67, 36, 78, 36, 81, 36, 67, 36, 81, 36, 81, 36, 82, 36, 69, 36, 66, 36, 66, 36, 54, 36, 39, 36, 78, 36, 78, 36, 78, 36, 62, 36, 57, 36, 73, 36, 75, 36, 69, 36, 76, 36, 81, 36, 74, 36

Converted integers:

0, 9275, 9256, 9239, 9270, 9270, 9257, 9262, 9266, 9268, 9295, 9295, 9282, 9281, 9274, 9274, 9274, 9261, 9267, 9281, 9281, 9292, 9294, 9285, 9270, 9284, 9302, 9301, 9297, 9297, 9296, 9300, 9300, 9296, 9298, 9301, 9297, 9296, 9291, 9291, 9295, 9307, 9299, 9280, 9287, 9295, 9272, 9254, 9263, 9267, 9257, 9264, 9279, 9272, 9266, 9273, 9283, 9294, 9297, 9283, 9297, 9297, 9298, 9285, 9282, 9282, 9270, 9255, 9294, 9294, 9294, 9278, 9273, 9289, 9291, 9285, 9292, 9297, 9290

the bytes i read from the file that i wrote from the above values:

32, 0, 0, 36, 59, 36, 40, 36, 23, 36, 54, 36, 54, 36, 41, 36, 46, 36, 50, 36, 52, 36, 79, 36, 79, 36, 66, 36, 65, 36, 58, 36, 58, 36, 58, 36, 45, 36, 51, 36, 65, 36, 65, 36, 76, 36, 78, 36, 69, 36, 54, 36, 68, 36, 86, 36, 85, 36, 81, 36, 81, 36, 80, 36, 84, 36, 84, 36, 80, 36, 82, 36, 85, 36, 81, 36, 80, 36, 75, 36, 75, 36, 79, 36, 91, 36, 83, 36, 64, 36, 71, 36, 79, 36, 56, 36, 38, 36, 47, 36, 51, 36, 41, 36, 48, 36, 63, 36, 56, 36, 50, 36, 57, 36, 67, 36, 78, 36, 81, 36, 67, 36, 81, 36, 81, 36, 82, 36, 69, 36, 66, 36, 66, 36, 54, 36, 39, 36, 78, 36, 78, 36, 78, 36, 62, 36, 57, 36, 73, 36, 75, 36, 69, 36, 76, 36, 81, 36

Converted integers:

32, 9216, 9275, 9256, 9239, 9270, 9270, 9257, 9262, 9266, 9268, 9295, 9295, 9282, 9281, 9274, 9274, 9274, 9261, 9267, 9281, 9281, 9292, 9294, 9285, 9270, 9284, 9302, 9301, 9297, 9297, 9296, 9300, 9300, 9296, 9298, 9301, 9297, 9296, 9291, 9291, 9295, 9307, 9299, 9280, 9287, 9295, 9272, 9254, 9263, 9267, 9257, 9264, 9279, 9272, 9266, 9273, 9283, 9294, 9297, 9283, 9297, 9297, 9298, 9285, 9282, 9282, 9270, 9255, 9294, 9294, 9294, 9278, 9273, 9289, 9291, 9285, 9292, 9297

UPDATE #3: Okay, so I made a small version of the programm, where I just read in bytes from a file converted them to integers, converted them back to bytes and wrote them to a file. Without the whole data format. And it actually worked. So the problem is with the algorithm and not with the byte to int (and back to byte) conversion. I will go back and check the algorithm. Thanks for the help :)

Jon Skeet
people
quotationmark

Yes - you're reading them in little-endian format (the first value is the least important) but you're writing them in big-endian format (the first value is the most important), assuming you're writing a then b.

Just reverse the order when you write them, assuming the file is meant to be little-endian:

byte a = (byte)(tempInt & 0xff);
byte b = (byte)((tempInt >> 8) & 0xff);

people

See more on this question at Stackoverflow