如何计算帧检查序列?

时间:2018-11-05 15:19:55

标签: javascript algorithm computer-science

我们通常通过CRC方法计算FCS。我们的计算机网络老师为我们提供了计算“ Hello World”的FCS的工作。

首先,我将每个字符转换为ASCII码,然后通过CRC方法计算FCS。

不幸的是,我错了。 “ hello world”的正确FCS是1010 0100 0100 1000 0100 1110 1111 1111

p(x)

enter image description here

我的代码

new Vue({
    el: "#app",
    data: {
       d: { a1:"", a2: "", a3: "", a3: "" },
       items: [],

       p: [1,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,1,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,1],//divisor 
       r: '00000000000000000000000000000000',
    },
    methods: {
        f: function() {
            let stringData = '';//dividend   Store as a string

            for (let c of this.d.a1 ) { //this.d.a1 is string data
                let subs = '000000000'+ c.charCodeAt().toString(2); 
                subs = subs.slice(subs.length-8,subs.length);//8-bit binary number
                stringData+=subs;
            }

            stringData+= this.r;//Add 32 zeros at the end

            //Remove leading 0
            // let cnt=0;
            // for (let x of stringData) {
            //     if (x=='0') cnt++;
            //     else break;
            // }
            // stringData=stringData.slice(cnt,stringData.length);

           

            let array = [...stringData];
            let left=0,right=32,maxLen=array.length,FCS='';
            //each time we use a numer X ( form left to right in array)  to simulate

            while (!!(right<maxLen)) {
                let target=array.slice(left,right+1);
                FCS='';
                //simulate ^ operation
                for (let i=0;i<=32;i++) {
                    if (target[i]==this.p[i]) FCS+='0';
                    else FCS+='1';
                }
                let index=1,pos=32;
                FCS=[...FCS];
                //if remainder has leading 0, handle it
                while ( !!(index<=32)) {
                    if (FCS[index]=='0') {
                        right++;
                        left++;
                        index++;
                        pos++;
                        FCS.push(array[right]);
                    }
                    else break;
                }
                let arrayPos=left+1;
                for (let i=index;i<=pos;i++) {
                    array[arrayPos++]=FCS[i];
                }
                left++;right++;
            }
            console.log(FCS.slice(1,).join(''));

        }
    }
})

0 个答案:

没有答案