如何从布尔数组计算CRC8中的校验和

时间:2014-05-05 15:40:36

标签: java arrays crc

我有一些带有一些网络数据包的布尔数组,需要计算CRC8,但出了点问题,我真的不知道是什么。

CRC8密钥: 100000111

输入: 1111111111111111111111111111111100000000

输出: 1000001000001000001000001000001011011000

应该: 1111111111111111111111111111111110111000

private static boolean[][] CRC8(boolean[][] packets, boolean[] CRC8Key) 
{
    boolean[]checksum=new boolean[CRC8Key.length-1];
    for(int i =0;i<packets.length;i++)
    {
        checksum=calculateChecksum(packets[i],CRC8Key);
        packets[i]=addCheckssumToPacket(packets[i],checksum);       
    }
    return packets;
}

private static boolean[] addCheckssumToPacket(boolean[] singlePacket,boolean[] checksum) {
    for(int i=0;i<checksum.length;i++)
    {
        singlePacket[singlePacket.length-(checksum.length-i)]=checksum[i];
    }

    return singlePacket;
}

protected static boolean[] calculateChecksum(boolean[] singlePacket, boolean[] CRC8Key) 
{       
    for (int i=0;i<32;i++)
    {
        if(singlePacket[i])
        {
            xorPartOfPacketWithKey(singlePacket, CRC8Key, i);
            System.out.println();
        }
    }
    return checksumm(singlePacket);
}

protected static boolean[] xorPartOfPacketWithKey(boolean[] singlePacket,boolean[] CRC8Key, int beginOfXor) 
{
    int k=0;
    for(int i=beginOfXor;i<beginOfXor+(CRC8Key.length);i++)
    {   
        singlePacket[i]=xor(singlePacket[i],CRC8Key[i-beginOfXor]);         
        k++;
    }
    return singlePacket;
}

protected static boolean[] checksumm(boolean[] singlePacket) {
    boolean[]checksum=new boolean[8];
    for (int i=0;i<8;i++)
    {
        checksum[i]=singlePacket[singlePacket.length-(8-i)];
    }
    return checksum;
}

0 个答案:

没有答案