曝光台 注意防骗
网曝天猫店富美金盛家居专营店坑蒙拐骗欺诈消费者
*
*/
/*
* This version is a signficant refactoring and rationalization of the class CRC.java
* released by Eurocontrol on 11/11/2004.
*
* Summary of changes by Mekon Ltd.
* <ul><li>the class is encapsulated so as to be serially reusable and not to
* require synchronization in a multi-threaded environment</li>
* <li>standard Java conventions for the naming of variables and methods have been
* adopted</li>
* <li>data structures declared but not used effectively have been removed</li>
* <li>data structures of length 33 have been normalized to length 32</li>
* <li>code for mapping between character and boolean representations has been
* simplified.</li>
* </ul>
*
* These changes to CRC.java are copyright (C) Mekon Ltd, 2004. Redistribution and
* use of this version is permitted under the terms specified by Eurocontrol as
* above, provided this second copyright notice is also retained (in the case of
* source code distribution) or reproduced (in the case of binary distribution).
*
*/
public class CRC {
/**
* The polynomial for CRC32 as an array of booleans. The least significant bit
* has the index 0.
*/
public final static boolean CRC32POLY[] = {
true, /* 1 */
true, /* x */
false, /* x2 */
true, /* x3 */
false, /* x4 */
true, /* x5 */
false, /* x6 */
true, /* x7 */
true, /* x8 */
false, /* x9 */
false, /* x10 */
false, /* x11 */
false, /* x12 */
false, /* x13 */
true, /* x14 */
false, /* x15 */
true, /* x16 */
42 Edition Number: 4.5
AIXM PRIMER
false, /* x17 */
false, /* x18 */
false, /* x19 */
false, /* x20 */
false, /* x21 */
true, /* x22 */
false, /* x23 */
true, /* x24 */
false, /* x25 */
false, /* x26 */
false, /* x27 */
false, /* x28 */
false, /* x29 */
false, /* x30 */
true /* x31 */
};
private final static int POLYNOMIALLENGTH = CRC32POLY.length;
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage CRC [string to calculate CRC]");
System.exit(1);
}
boolean[] crc = computeCRC(args[0]);
show(crc);
}
/**
*
* Computes a CRC value for the given input. Returns an array of booleans.
* @param input
*
*/
private static boolean[] computeCRC(String input) {
char[] toProcess = input.toCharArray();
boolean[] toReturn = new boolean[POLYNOMIALLENGTH];
for (int z = 0; z < input.length(); z++) {
boolean[] characterAsBoolean = charToBoolean(toProcess[z]);
processChar(toReturn, characterAsBoolean);
}
return reverse(toReturn);
}
/**
* Processes the next character from the input.
*/
private static void processChar(boolean[] remainder, boolean[] characterAsBoolean)
{
// remainder is always of length 32.
for (int k = 0; k < 8; k++) {
int count = POLYNOMIALLENGTH-1;
boolean value = remainder[count] ^ characterAsBoolean[k];
for (;count > 0;count--) {
if (CRC32POLY[count] == true) {
remainder[count] = remainder[count - 1] ^ value;
} else {
remainder[count] = remainder[count - 1];
Edition Number: 4.5 43
AIXM PRIMER
}
}
// notice that count at this point is always zero.
remainder[count] = value;
}
}
/**
* Computes a hexadecimal CRC value for the supplied input. Returns the
* hexadecimal representation of the CRC for the supplied input. The result will
* contain no lower case letters. (Cf AIXM definition of "Data Types for
* Cyclic Redundancy Check Values (CRCV)".)
* @param input
* @return
*/
public static String computeCRCHex(String input) {
String a = booleanToHex(reverse(CRC32POLY));
boolean[] raw = computeCRC(input);
return booleanToHex(raw);
}
/**
* Reverses the booleans in an array.
*/
private static boolean[] reverse(boolean[] toProcess) {
boolean[] toReturn = new boolean[toProcess.length];
for (int k = toProcess.length-1; k > -1; k--) {
toReturn[(toProcess.length-1)-k] = toProcess[k];
}
return toReturn;
}
// only used in main()
private static void show(boolean[] crcValue) {
String buf = "";
System.out.print("Resultat : ");
for (int j = 0; j < crcValue.length; j++) {
if (crcValue[j] == true)
buf += "1";
else
buf += "0";
}
System.out.println(buf);
System.out.println(booleanToHex(crcValue));
中国航空网 www.aero.cn
航空翻译 www.aviation.cn
本文链接地址:
AIXM_Primer_4.5(20)