Giter Site home page Giter Site logo

zenuo / java-crc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snksoft/java-crc

0.0 1.0 0.0 15 KB

Generic CRC implementation for java language (includes CRC16, CRC32, CRC64 etc)

License: BSD 3-Clause "New" or "Revised" License

Java 47.31% Groovy 52.69%

java-crc's Introduction

This package implements generic CRC calculations up to 64 bits wide. It aims to be fairly fast and fairly complete, allowing users to match pretty much any CRC algorithm used in the wild by choosing appropriate Parameters. This obviously includes all popular CRC algorithms, such as CRC64-ISO, CRC64-ECMA, CRC32, CRC32C, CRC16, CCITT, XMODEM and many others. See http://reveng.sourceforge.net/crc-catalogue/ for a good list of CRC algorithms and their parameters.

This package has been largely inspired by Ross Williams' 1993 paper "A Painless Guide to CRC Error Detection Algorithms".

Usage

Using src is easy. Here is an example of calculating CCITT crc.

import com.github.snksoft.crc.CRC;

public class Example
{
    public static void main(String [] args) {
        String data = "123456789";
        long ccittCrc = CRC.calculateCRC(CRC.Parameters.CCITT, data.getBytes());
        System.out.printf("CRC is 0x%04X\n", ccittCrc); // prints "CRC is 0x29B1"

    }
}

For larger data, table driven implementation is faster. Here is how to use it.

import com.github.snksoft.crc.CRC;

public class Example
{
    public static void main(String [] args) {
        String data = "123456789";
       	CRC tableDriven = new CRC(CRC.Parameters.XMODEM);
       	long xmodemCrc = tableDriven.calculateCRC(data.getBytes());
        System.out.printf("CRC is 0x%04X\n", xmodemCrc); // prints "CRC is 0x31C3"

       	// You can also reuse CRC object instance for another crc calculation.
        // Given that the only state for a CRC calculation is the "intermediate value"
        // and it is stored in your code, you can even use same CRC instance to calculate CRC
        // of multiple data sets in parallel.
       	// And if data is too big, you may feed it in chunks
       	long curValue = tableDriven.init(); // initialize intermediate value
       	curValue = tableDriven.update(curValue, "123456789".getBytes()); // feed first chunk
        curValue = tableDriven.update(curValue, "01234567890".getBytes()); // feed next chunk
       	long xmodemCrc2 = tableDriven.finalCRC(curValue); // gets CRC of whole data ("12345678901234567890")
        System.out.printf("CRC is 0x%04X\n", xmodemCrc2); // prints "CRC is 0x2C89"
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.