java实现CRC16校验码生成

public static String crc16(String gprsstr) {
		try {
			int crc;
			int strlength, r;
			byte sbit;
			int tc;
			strlength = gprsstr.length();
			byte[] data = gprsstr.getBytes();
			crc = 0x0000FFFF;
			for (int i = 0; i < strlength; i++) {
				tc = (int) (crc >>> 8);
				crc = (int) (tc ^ data[i]);
				for (r = 0; r < 8; r++) {
					sbit = (byte) (crc & 0x00000001);
					crc >>>= 1;
					if (sbit != 0)
						crc ^= 0x0000A001;
				}
			}
			return Integer.toHexString(crc);
		} catch (Exception ex) {
			return "";
		}
	}

你可能感兴趣的:(JAVA项目开发)