Calculate CRC32 from digit - how? #11

Closed
opened 2018-05-26 18:41:24 +00:00 by saewave · 3 comments
saewave commented 2018-05-26 18:41:24 +00:00 (Migrated from github.com)

Hi!

How I can calculate CRC32 from 32 bit hex digit like 0x200031E0 (CRC should be 0x0AAFD11A) or from 32 bit dec?

Hi! How I can calculate CRC32 from 32 bit hex digit like 0x200031E0 (CRC should be 0x0AAFD11A) or from 32 bit dec?
reviewher commented 2018-05-26 19:11:46 +00:00 (Migrated from github.com)

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?

`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: ```js 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`?
saewave commented 2018-05-27 17:16:22 +00:00 (Migrated from github.com)

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.

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.
michau-krakow commented 2019-07-25 18:46:45 +00:00 (Migrated from github.com)

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.

> 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.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/js-crc32#11
No description provided.