Reading an int from bytes(binary file) - HELP
Code Java:
public static int ReadInt(FileInputStream f)
{
try
{
int x = 0;
int b = f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
}
catch (Exception e) {
}
return x;
}
getting 0 all the time.....
Re: Reading an int from bytes(binary file) - HELP
For future reference, please flank your code with the [highlight=java][/highlight] tags.
What input are you reading? It might help if you could post a more clear, compilable, concise, example that demonstrates the function(s) working improperly with a defined input.
Re: Reading an int from bytes(binary file) - HELP
Code :
public static void main(String[] args){
system.out.println(ReadInt(f));//f is a bmp file
}
public static int ReadInt(FileInputStream f)
{
try
{
int x = 0;
int b = f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
x = x << 8;
b=f.read();
x += b;
}
catch (Exception e) {
}
return x;
}
Re: Reading an int from bytes(binary file) - HELP
Better, but no one else has access to the contents of f...try modifying the code so that it demonstrates the behavior with a given series of known bytes (hard coded)