01: package de.mcs.utils;
02:
03: /*
04: * Copyright (c) 2004 by MCS Software --------------------------------------
05: * Created on 04.01.2005 by w.klaas
06: */
07: /**
08: * Build 16 bit checksum of some values.
09: *
10: * @author w.klaas
11: */
12: public final class Checksum16 {
13:
14: /** private constructor to prevent instancing. */
15: private Checksum16() {
16: };
17:
18: /**
19: * parses the string and build the 16 bit checksum.
20: *
21: * @param str
22: * string to build checksum from
23: * @return int 16 bit checksum
24: */
25: public static int parseString(final String str) {
26: return parseArray(str.getBytes());
27: }
28:
29: /**
30: * parses the byte array and build the 16 bit checksum.
31: *
32: * @param array
33: * array to build checksum from
34: * @return int 16 bit checksum
35: */
36: public static int parseArray(final byte[] array) {
37: int lo = 0;
38: int hi = 0;
39: for (int n = 0; n < array.length; n++) {
40: if ((n % 2) == 0) {
41: lo = lo ^ array[n];
42: } else {
43: hi = hi ^ array[n];
44: }
45: }
46: return lo + (hi << 8);
47: }
48: }
|