⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkcrc.java

📁 JRed is a 100% Java implementation of the IrDA infrared communications protocols. JRed lets you beam
💻 JAVA
字号:
/*
**************************************************************************
** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/irlap/CheckCRC.java,v 1.1.1.1 2000/07/05 04:41:52 mpatters Exp $
**
** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
**
** This file is part of JRed, a 100% Java implementation of the IrDA
** infrared communications protocols.
**
** This file may be distributed under the terms of the Synchrona Public
** License as defined by Synchrona, Inc. and appearing in the file
** LICENSE included in the packaging of this file. The Synchrona Public
** License is based on the Q Public License as defined by Troll Tech AS
** of Norway; it differs only in its use of the courts of Florida, USA
** rather than those of Oslo, Norway.
**************************************************************************
*/
package com.synchrona.jred.irlap;

class CheckCRC {
	public static void main(String [] astrArgs) {
		CheckCRC c       = new CheckCRC();
		byte []  ayBytes = new byte[32];

		for ( int i = 0; i < ayBytes.length; i++ ) {
			ayBytes[i] = (byte) i;
		}

		try {
			int fcs = c.getCRC(ayBytes, 0, ayBytes.length);
			System.out.println("fcs: " + fcs);
		} catch ( Exception e ) {
			System.err.println(e);
			e.printStackTrace(System.err);
		}
	}

	public CheckCRC() {
		if ( CrcTable.ayFcsTable.length != 256 ) {
			System.err.println("FCS table should have 256 items, had " + CrcTable.ayFcsTable.length);
		}
	}

	public short getCRC(byte [] ayBytes, int nOffset, int nLength) throws Exception {

		int fcs = 0x0000FFFF;

		// Make sure the input FCS contains only 16 bits worth of data
		if ( (0xFFFF0000 & fcs) != 0 ) {
			throw new Exception("Input CRC contained more than 16 bits of data: " + fcs);
		}

		int i = nOffset;
		while ( nLength-- > 0 ) {
			int term1 = fcs >>> 8;
			if ( term1 > 0x0000FFFF ) {
				throw new Exception("CRC term went out of 16-bit range...this is a bug");
			}

			int term2 = (fcs ^ ayBytes[i++]) & 0x000000FF;
			if ( term2 > 0x000000FF ) {
				throw new Exception("CRC index exceeds 255...this is a bug");
			}

			int term3 = CrcTable.ayFcsTable[term2] & 0x0000FFFF;
			if ( term3 < 0 ) {
				throw new Exception("CRC term went negative...this is a bug");
			}

			fcs = (term1 ^ term3);
		}

		// The IrLAP Frame Check Sequence is the one's-complement of the
		// CRC.
		return (short) ~(fcs & 0x0000FFFF);
	}

	public String printBits(int val, int width) {
		StringBuffer buf = new StringBuffer();
		for ( int i = width - 1; i >= 0; i-- ) {
			if ( (val & (1 << i)) != 0 ) {
				buf.append("1");
			} else {
				buf.append("0");
			}
		}
		return new String(buf);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -