Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/07/2008, 06:27
bonethugs
 
Fecha de Ingreso: julio-2008
Mensajes: 8
Antigüedad: 15 años, 10 meses
Puntos: 0
Sonrisa Programa CRC 2

El siguiente programa calcula el CRC16 CCITT de los valores introducidos por teclado.

Lo que quiero saber es si alguien me podria ayudar a modificarlo de tal forma que el programa en vez de coger los valores por teclado, se le pueda introducir el nombre de un fichero con los datos para calcular su CRC.

Muchas gracias a todos!!!


/************************************************** ***********************
* Compilation: javac CRC16CCITT.java
* Execution: java CRC16CCITT s
* Dependencies: StdIn.java
*
* Reads in a sequence of bytes and prints out its 16 bit
* Cylcic Redundancy Check (CRC-CCIIT 0xFFFF).
*
* 1 + x + x^5 + x^12 + x^16 is irreducible polynomial.
*
* % java CRC16-CCITT 123456789
* CRC16-CCITT = 29b1
*
************************************************** ***********************/

public class CRC16CCITT {

public static void main(String[] args) {
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)

// byte[] testBytes = "123456789".getBytes("ASCII");

byte[] bytes = args[0].getBytes();

for (byte b : bytes) {
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}

crc &= 0xffff;
System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
}

}