java - Unable to correctly generate checksum -
i trying calculate simple checksum , i'm doing checksum test.
public void readfile() { byte counter = 0; byte[] data = new byte[68]; while(x.hasnextint()) { data[counter++] = (byte)x.nextint(); if(counter == 67) break; } int sum = 0; (int = 0; < 67; i++) sum += (data[i] & 0xff); system.out.println((int)(sum & 0xff)); //checksum //perform checksum test. data[counter] = (byte)(sum & 0xff); sum = 0; (int = 0; < 68; i++) sum += (data[i] & 0xff); system.out.println((int)(sum & 0xff)); //checksum test }
i reading first 67 integer values of file , saving them inside byte array. integers < 256. variable 'x' file reading. however, when perform checksum test, value outputted isn't 0. not able diagnose computing incorrectly.
i'm not sure you're trying do. first 67 characters, calculate checksum, , assume 67, plus checksum, 0. not. you're adding checksum, not subtracting it.
so if checksum not 0 or 128, end value twice checksum. right?
Comments
Post a Comment