Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Reading an int from bytes(binary file) - HELP

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Reading an int from bytes(binary file) - HELP

    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.....
    Last edited by helloworld922; December 14th, 2010 at 06:08 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default 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.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Reading an int from bytes(binary file) - HELP

     
     
    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;
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 828 Times in 771 Posts
    Blog Entries
    5

    Default 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)
    Last edited by copeg; December 14th, 2010 at 01:06 PM.

Similar Threads

  1. Reading a writing binary file
    By stu1811 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: July 30th, 2010, 12:58 PM
  2. Creating a binary dump of a file
    By NRDargie in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 20th, 2010, 05:24 PM
  3. How to write 2 dimensional array of float numbers to binary file?
    By Ghuynh in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 17th, 2010, 04:26 PM
  4. How to save statistics of a game in a binary file instead of txt file?
    By FretDancer69 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: June 19th, 2009, 05:05 AM
  5. How to Get the size of a file in bytes
    By JavaPF in forum File Input/Output Tutorials
    Replies: 1
    Last Post: June 8th, 2009, 10:19 AM