Languages
[Edit]
EN

TypeScript - calculate crc32

3 points
Created by:
cory
1486

In this short article, we would like to show how in a simple way calculate CRC32 sum using TypeScript.

Practical example:

const CRC_TABLE: number[] = Array(256);

for (let i = 0; i < 256; ++i) {
    let code = i;
    for (let j = 0; j < 8; ++j) {
        code = code & 0x01 ? 0xedb88320 ^ (code >>> 1) : code >>> 1;
    }
    CRC_TABLE[i] = code;
}

const crc32 = (text: string): number => {
    let crc = -1;
    for (let i = 0; i < text.length; ++i) {
        const code = text.charCodeAt(i);
        crc = CRC_TABLE[(code ^ crc) & 0xff] ^ (crc >>> 8);
    }
    return (-1 ^ crc) >>> 0;
};


// Usage example:

console.log(crc32('This is example text ...'));  // 3473739588

Output:

3473739588

 

See also

  1. JavaScript - calculate crc32 
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join