title: python to Java checksum calculation
body: <p>I received this python script that generates a file checksum:</p>
  
  <pre><code>import sys,os
  
  if __name__=="__main__":
  #filename=os.path.abspath(sys.argv[1])
  #filename=r"H:\Javier Ortiz\559-7 From Pump.bin"
  cksum=0
  offset=0
  pfi=open(filename,'rb')
  while 1:
    icks=0
    chunk=pfi.read(256)
    if not chunk:  break     #if EOF exit loop
  
    for iter in chunk:
      icks+=ord(iter)
      print ord(iter)
    cksum=(cksum+icks) &amp; 0xffff
  pfi.close()
  print "cksum=0x%4.4x"%cksum
  </code></pre>
  
  <p>And I'm trying to convert it to Java but I'm not geting the same results.</p>
  
  <p>Here's my Java code:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.DataInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  
  public class ChecksumCalculator {
  
  private ChecksumCalculator() {
  }
  
  public static int getChecksum(File file) {
      int cksum = 0;
      FileInputStream fis = null;
      BufferedInputStream bis = null;
      DataInputStream dis = null;
      try {
          fis = new FileInputStream(file);
  
          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
          byte[] buffer = new byte[256];
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.read(buffer) != -1) {
              int icks = 0;
              for (byte b : buffer) {
                  icks += b &amp; 0xff;
                  System.out.println(b &amp; 0xff);
              }
              cksum = (cksum + icks) &amp; 0xffff;
              System.out.println("Checksum: " + cksum);
          }
  
          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();
          return cksum;
      } catch (FileNotFoundException e) {
          e.printStackTrace();
          return -1;
      } catch (IOException e) {
          e.printStackTrace();
          return -1;
      }
  }
  
  static public void main(String[] s) {
      System.out.println("0x" + getChecksum(new File("H:\\Javier Ortiz\\559-7 From Pump.bin")));
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But I get different results on a file. For example if I run it on a plain txt file containing only the word test it gives out the following result:</p>
  
  <p>python: cksum=0x01c0
  java: cksum=0x448</p>
  
  <p>Any idea?</p>