convert crc javascript func to c$ func

106 views Asked by At

i have a problem whit c#, please someone convert this javascript function to c#.

function mycrc(str) {
        var c;
        var crcTable = [];
        for(var n = 0; n < 256; n++){
            c = n;
            for(var k = 0; k < 8; k++){
                c = ((c&1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
            }
            crcTable[n] = c;
        }



    var crc = 0 ^ (-1);
    for (var i = 0; i < str.length; i++ ) {
        crc = (crc >>> 8) ^ crcTable[(crc ^ str.charCodeAt(i)) & 0xFF];
    }
    return (crc ^ (-1)) >>> 0;
}
1

There are 1 answers

4
John Smith On
uint mycrc(string str)
{
    unchecked {
        uint c;
        uint[] crcTable = new uint[256];
        for (uint n = 0; n < 256; n++) {
            c = n;
            for (var k = 0; k < 8; k++) {
                c = ((c & 1) != 0) ? (0xEDB88320 ^ (c >> 1)) : (c >> 1);
            }
            crcTable[n] = c;
        }



        uint crc = 0 ^ (uint)(-1);
        for (var i = 0; i < str.Length; i++) {
            crc = (crc >> 8) ^ crcTable[(crc ^ str[i]) & 0xFF];
        }
        return crc ^ (uint)(-1);
    }
}

THANK