Git Product home page Git Product logo

Comments (3)

saewave avatar saewave commented on May 21, 2024 1

Thank you, I did something similar you provided, but it would be great to have additional function like CRC32.int or CRC32.hex to calculate CRC without extra code.

How did you get that CRC result 0x0AAFD11A?

You are right, the result should be 0xe9fc314b it was my mistake.

from js-crc32.

michau-krakow avatar michau-krakow commented on May 21, 2024 1

additional function like CRC32.int or CRC32.hex to calculate CRC without extra code

C'mon, splitting number into 4 bytes isn't worth any library support, and calculating 32-bit crc from 32-bit number does not seem to be very common use case.

from js-crc32.

reviewher avatar reviewher commented on May 21, 2024

CRC32.buf can take an array of bytes and CRC32.bstr can take a binary string. Both return a signed 32-bit integer that you can convert to hex through unsigned conversion >>>0 and toString(16).

To convert the 32-bit number to bytes you can either unwind the bytes manually or use a Uint32Array:

var num = 0x200031E0;

/* Uint8Array */
var ua = new Uint8Array((new Uint32Array([num])).buffer); // Uint8Array [ 224, 49, 0, 32 ]
(CRC32.buf(ua)>>>0).toString(16); // e9fc314b

/* Array of bytes */
var arr = [], n2 = num; for(var i = 0; i < 4; ++i, n2>>>=8) arr[i] = n2 & 0xFF; // arr = [ 224, 49, 0, 32 ]
(CRC32.buf(arr)>>>0).toString(16); // e9fc314b

/* Binary string */
var bstr = ""; for(i = 0, n2 = num; i < 4; ++i, n2>>>=8) bstr += String.fromCharCode(n2 & 0xFF); // bstr = '\xe0\x31\x00\x20'
(CRC32.bstr(bstr)>>>0).toString(16); // e9fc314b

The result is determined to be 0xe9fc314b. How did you get that CRC result 0x0AAFD11A?

from js-crc32.

Related Issues (17)

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.