Giter VIP home page Giter VIP logo

nullfx.crc's Introduction

NullFX CRC build Quality Gate Status

NullFX CRC is a small set of CRC utilities written in native C# released under the MIT License

NuGet:

GitHub Package Page

NuGet.org Package Page

Install

dotnet add PROJECT package NullFX.CRC --version 1.1.10

Examples:

Each CRC library uses a common ComputeChecksum format. It accepts a byte array which can be obtained by converting text / numbers / structures etc into an array, then passing the byte array into ComputeChecksum for it's CRC.

The ComputeChecksum method also has a params argument parameter allowing individual bytes to be passed into the method one at a time rather than as an array.

If a checksum needs to be performed on a segment of an array, rather than creating a copy of the array to perform the CRC on, you can pass in the entire buffer and specify the section of the array in which to perform the CRC calculation, saving time and memory:

// using text
var text = "I am string content";
// convert text to a byte array
var textBuffer = System.Text.Encoding.UTF8.GetBytes ( text );

// get the CRC for the text
var textCrc = NullFX.CRC.Crc32.ComputeChecksum ( textBuffer );
Console.WriteLine ( "Text CRC: {0:X8}", textCrc );


// use a large number
var aNumber = 0xDEADBEEF;
// convert that to a byte array
var numberBuffer = System.BitConverter.GetBytes ( aNumber );

// get the CRC for the number
var numberCrc = NullFX.CRC.Crc32.ComputeChecksum ( numberBuffer );
Console.WriteLine ( "Number CRC: {0:X8}", numberCrc );


// bytes as params
var randomCrc = NullFX.CRC.Crc32.ComputeChecksum ( 0x01, 0x02, 0x03, 0x04 );
Console.WriteLine ( "Random bytes CRC: {0:X8}", randomCrc );


/// checksum of a subset of an array. perform the CRC on bytes at indices
// 2, 3, 4 and 5
var bytes = new byte[] { 0xFE, 0x2C, 0xED, 0x4B, 0x88, 0x31, 0x07, 0xBE };
var segmentedBytesCrc = Crc32.ComputeChecksum ( bytes, 2, 4 );
Console.WriteLine ( "Segment of bytes CRC: {0:X8}", segmentedBytesCrc );

Output:

Text CRC: 3AD00FD2
Number CRC: 1A5A601F
Random bytes CRC: B63CFBCD
Segment of bytes CRC: DB1A36A1

API Information

Crc8, and Crc32's ComputeChecksum have 2 different signatures

ComputeChecksum(byte[] bytes)

and

ComputeChecksum(byte[] bytes, int start, int length )

Crc16 has one additional initial parameter ( Crc16Algorithm ) where Crc16Algorithm is one of the following:

  • Standard CRC 16
  • CRC 16 CCITT with initial values of 0, FFFF and 1D0F
    • CRC 16 CCITT Kermit
  • Modbus

ComputeChecksum ( Crc16Algorithm algorithm, byte[] bytes )

and

ComputeChecksum ( Crc16Algorithm algorithm, byte[] bytes, int start, int length )

.

Note: this repository is also mirrored on GitLab

nullfx.crc's People

Contributors

johannesdeml avatar jtone123 avatar nullfx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

nullfx.crc's Issues

Variations of CRC

I've tested out all of the algorithms, and I've been comparing them to CyberChef since that's the app I use the most (via https://gchq.github.io/CyberChef/)

Is CyberChef using a different type of CRC algo? Or am I missing a vital setting in yours. Because I've tested out about every variation of CRC16 I can, and I never get the values that I get in CyberChef.

However, CRC32 seems to be fine.

Result value small and capital character are the same? How to get return value with small character?

// using text
var text = "I am string content";
// convert text to a byte array
var textBuffer = System.Text.Encoding.UTF8.GetBytes ( text );
// get the CRC for the text
var textCrc = NullFX.CRC.Crc32.ComputeChecksum ( textBuffer );
Console.WriteLine ( "Text CRC: {0:X8}", textCrc );

How to get the result string with small character instead of capital character.
for "I am string content"
3ad00fd2 instad of value "3AD00FD2"
I check from online this site the result are return with small character CRC32 value.

NullFX.CRC.Crc16.ComputeChecksum(Crc16Algorithm.CcittKermit, bytes) gving wrong results

Hello, I try to use the Crc16Algorithm.CcittKermit, but its giving me wrong results.

var rawValue = 3218461.ToString();
var bytes = Encoding.UTF8.GetBytes(rawValue);
var raw = NullFX.CRC.Crc16.ComputeChecksum(Crc16Algorithm.CcittKermit, bytes);
var a = string.Format("0x{0:X}", NullFX.CRC.Crc16.ComputeChecksum(Crc16Algorithm.CcittKermit, bytes));

a result = 0xAAE0
But checking on https://crccalc.com/?crc=3218461&method=crc16&datatype=ascii&outtype=0 the result should be 0xE0AA

As you can see, the correct characters are there, just not in the right order and this happens for every input I throw at it. So my quickfix was just to do this: var fixedValue = $"{a[0]}{a[1]}{a[4]}{a[5]}{a[2]}{a[3]}"; But it would be nice if the correct output comes out of the ComputedCheckSum methods.

I am not sure if I am doing something wrong with the package or that there is a bug? I have the feeling I am doing something wrong, but I have no experience in this area.

EDIT: fix for ComputeChecksum

raw = SwapBytes(raw );
var a = string.Format("0x{0:X}", raw );

public static ushort SwapBytes(ushort x)
      {
          byte[] bytes = BitConverter.GetBytes(x);

          Array.Reverse(bytes);
          return BitConverter.ToUInt16(bytes, 0);
      }

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.