Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ CRC计算模板适用于除CRC8以外的所有类型_C++_Templates_Crc - Fatal编程技术网

C++ CRC计算模板适用于除CRC8以外的所有类型

C++ CRC计算模板适用于除CRC8以外的所有类型,c++,templates,crc,C++,Templates,Crc,我不知道这里是问这个问题的合适地方,还是应该在codereview上问这个问题。无论如何,我已经编写了以下代码来计算各种类型的CRC。CRC16、CRC32和CRC64的结果与在线实现(如和)匹配。但是对于CRC8,无论我设置了参数,结果都不匹配。我不是循环冗余检查细节方面的专家,只是读了维基百科文章的一部分。谁能告诉我出了什么问题吗 #包括 #包括 #包括 命名空间crc { 命名空间模板 { 模板结构crcdata { T数=0; std::字符串到十六进制(void) { std::字符串

我不知道这里是问这个问题的合适地方,还是应该在codereview上问这个问题。无论如何,我已经编写了以下代码来计算各种类型的CRC。CRC16、CRC32和CRC64的结果与在线实现(如和)匹配。但是对于CRC8,无论我设置了参数,结果都不匹配。我不是循环冗余检查细节方面的专家,只是读了维基百科文章的一部分。谁能告诉我出了什么问题吗

#包括
#包括
#包括
命名空间crc
{
命名空间模板
{
模板结构crcdata
{
T数=0;
std::字符串到十六进制(void)
{
std::字符串s(2*sizeof(T),'0');
对于(tn=number,i=s.size();n;n>>=4)
s[--i]+=(n&0xF)>9?(n%16-9)| 16:n%16;
返回s;
}
};
模板
普通类
{
公众:
内联通用_crc()
{
静态T表[256];
///生成CRC查找表。如果已计算循环,则跳过循环
对于(inti=0,b=0;i<256&&!表[255];b=8,i++)
{
表[i]=i;
而(b--)表[i]=(表[i]>>1)^(表[i]&1?多项式:0);
}
此->result.number=init\u cr;
这->crc_表=(T常量*)(无效*)和表[0];
}
虚~general_crc(){}
私人:
T const*crc_表;
crcdata结果;
无效循环计算(常量无效*buf,大小)
{
uint8_t*p=(uint8_t*)buf;
而(大小--)
this->result.number=this->crc_表[(this->result.number^*p++)和0xFF]^(this->result.number>>8);
}
公众:
///字符串的crc
静态crcdata计算(常量std::string&s)
{
一般情况;
cr.crc_计算(s.c_str(),s.size());
cr.result.number^=最终结果;
返回cr.result;
}
};
}
typedef模板::general_crc CRC8;
typedef模板::general_crc CRC16;
typedef模板::general_crc CRC32;
typedef模板::general_crc CRC64;
}
#包括
int main()
{
std::string test=“这是一个测试!!”;

std::cout代码或结果没有问题。你认为你应该得到什么,为什么?

我检查我的结果。如果CRC8不匹配。关于你问题的第二部分,我不知道我应该得到什么。我只是用一些已经编写的代码交叉检查它们。是的,它匹配。你需要输入我n多项式反映为
0xd5
。谢谢。它起作用了。现在我有另一个问题。“反映的输入”和“反映的结果”是什么意思?我如何实现这些选项?你应该发布一个新问题。这些评论不是针对一系列问题和答案的。这很公平。@Mark Adler
#include <cstdio>
#include <string>
#include <stdint.h>

namespace crc
{
    namespace templates
    {
        template <typename T> struct crcdata
        {
            T number = 0;
            std::string toHex(void)
            {
                std::string s(2 * sizeof(T), '0');
                for (T n = number, i = s.size(); n; n >>= 4)
                    s[--i] += (n & 0xF) > 9 ? (n % 16 - 9) | 16 : n % 16;

                return s;
            }
        };

        template <typename T, T polynomial, T init_cr, T final_cr>
        class general_crc
        {
        public:
            inline general_crc()
            {
                static T table[256];

                /// build CRC lookup table. Skip the loop if already evaluated
                for (int i = 0, b = 0; i < 256 && !table[255]; b = 8, i++)
                {
                    table[i] = i;
                    while (b--) table[i] = (table[i] >> 1) ^ (table[i] & 1 ? polynomial : 0);
                }

                this->result.number = init_cr;
                this->crc_table = (T const*)(void*)&table[0];
            }

            virtual ~general_crc(){}

        private:
            T const* crc_table;
            crcdata <T> result;

            void crc_calc(const void* buf, size_t size)
            {
                uint8_t* p = (uint8_t*)buf;

                while (size--)
                    this->result.number = this->crc_table[(this->result.number ^ *p++) & 0xFF] ^ (this->result.number >> 8);
            }

        public:
            /// crc of string
            static crcdata <T> calculate(const std::string& s)
            {
                general_crc cr;
                cr.crc_calc(s.c_str(), s.size());
                cr.result.number ^= final_cr;
                return cr.result;
            }
        };
    }

    typedef templates::general_crc <uint8_t,    0xAB, 0, 0> CRC8;
    typedef templates::general_crc <uint16_t,   0xA001, 0, 0>   CRC16;
    typedef templates::general_crc <uint32_t,   0xEDB88320U, 0xFFFFFFFFU, 0xFFFFFFFFU>  CRC32;
    typedef templates::general_crc <uint64_t,   0xC96C5795D7870F42LLU, ~0LLU, ~0LLU>    CRC64;
}

#include <iostream>
int main()
{
    std::string test = "This is a test!!";
    std::cout << crc::CRC8::calculate(test).toHex() << '\n';
    std::cout << crc::CRC16::calculate(test).toHex() << '\n';
    std::cout << crc::CRC32::calculate(test).toHex() << '\n';
    std::cout << crc::CRC64::calculate(test).toHex() << '\n';
    return 0;
}