crc16定义
typedef crc_optimal<16, 0x8005, 0, 0, true, true> crc_16_type;
template < std::size_t Bits, BOOST_CRC_PARM_TYPE TruncPoly,
BOOST_CRC_PARM_TYPE InitRem, BOOST_CRC_PARM_TYPE FinalXor,
bool ReflectIn, bool ReflectRem >
class crc_optimal
{
// Implementation type
typedef detail::mask_uint_t<Bits> masking_type;
public:
// Type
typedef typename masking_type::fast value_type;
// Constants for the template parameters
BOOST_STATIC_CONSTANT( std::size_t, bit_count = Bits );
BOOST_STATIC_CONSTANT( value_type, truncated_polynominal = TruncPoly );
BOOST_STATIC_CONSTANT( value_type, initial_remainder = InitRem );
BOOST_STATIC_CONSTANT( value_type, final_xor_value = FinalXor );
BOOST_STATIC_CONSTANT( bool, reflect_input = ReflectIn );
BOOST_STATIC_CONSTANT( bool, reflect_remainder = ReflectRem );
// Constructor
explicit crc_optimal( value_type init_rem = InitRem );
// Internal Operations
value_type get_truncated_polynominal() const;
value_type get_initial_remainder() const;
value_type get_final_xor_value() const;
bool get_reflect_input() const;
bool get_reflect_remainder() const;
value_type get_interim_remainder() const;
void reset( value_type new_rem = InitRem );
// External Operations
void process_byte( unsigned char byte );
void process_block( void const *bytes_begin, void const *bytes_end );
void process_bytes( void const *buffer, std::size_t byte_count );
value_type checksum() const;
// Operators
void operator ()( unsigned char byte );
value_type operator ()() const;
private:
// The implementation of output reflection depends on both reflect states.
BOOST_STATIC_CONSTANT( bool, reflect_output = (ReflectRem != ReflectIn) );
#ifndef __BORLANDC__
#define BOOST_CRC_REF_OUT_VAL reflect_output
#else
typedef crc_optimal self_type;
#define BOOST_CRC_REF_OUT_VAL (self_type::reflect_output)
#endif
// More implementation types
typedef detail::crc_table_t<Bits, TruncPoly, ReflectIn> crc_table_type;
typedef detail::crc_helper<Bits, ReflectIn> helper_type;
typedef detail::crc_helper<Bits, BOOST_CRC_REF_OUT_VAL> reflect_out_type;
#undef BOOST_CRC_REF_OUT_VAL
// Member data
value_type rem_;
}; // boost::crc_optimal
crc_optimal模板参数
- Bits:CRC校验位数
- TruncPoly: 多项式,由于除数总是比余数大一位,而作为除数的多项式最高位总是为1。在描述CRC多项式时,最高位被忽略,这样可以让多项式(除数)和CRC校验和(余数)使用同一数据类型来表示。
- InitRem: 初始值
- FinalXor: 最后的异或值,返回值是校验和与之异或的值。
- ReflectIn: 反转输入值
- ReflectRem: 反转输出值
测试
#include <string.h>
#include <iostream>
#include <boost/crc.hpp>
int main()
{
//发送方
boost::crc_16_type crc16_send;
char* send_data="1234567890";
int len = strlen(send_data);
crc16_send.process_bytes(send_data,len);
unsigned short send_crc_checksum1 = crc16_send.checksum();
std::cout<<"send_crc_checksum1="<<std::hex<<std::showbase<<send_crc_checksum1 <<std::endl;
//接收方
char buff[12] = {0};
memcpy(buff,send_data,strlen(send_data));
memcpy(&buff[10],&send_crc_checksum1,sizeof(send_crc_checksum1 ));
boost::crc_16_type crc16_recv;
crc16_recv.process_bytes(buff,sizeof(buff));
unsigned short recv_crc_checksum1 = crc16_recv.checksum();
std::cout<<"recv_crc_checksum1="<<std::hex<<std::showbase<<recv_crc_checksum1 <<std::endl;
if(recv_crc_checksum1 == 0)
{
std::cout<<"crc data have no error,crc check successfully!"<<std::endl;
}
else
{
std::cout<<"crc data have error,crc check failed!"<<std::endl;
}
return 0;
}
[banting@localhost test]$ g++ test_boost_crc.cpp -L/usr/local/lib -lboost_system -o test_boost_crc
[banting@localhost test]$ ./test_boost_crc
send_crc_checksum1=0xc57a
recv_crc_checksum1=0
crc data have no error,crc check successfully!
参考文献
https://blog.csdn.net/u014421422/article/details/43087231?utm_medium=distribute.pc_relevant.none-task-blog-2defaultbaidujs_baidulandingword~default-0-43087231-blog-116930441.235v38pc_relevant_sort&spm=1001.2101.3001.4242.1&utm_relevant_index=3
https://www.boost.org/doc/libs/1_42_0/libs/crc/crc.html#header
https://www.cnblogs.com/skullboyer/p/8342167.html