1. 程式人生 > 其它 >CRC16 校驗和計算

CRC16 校驗和計算

技術標籤:python機器學習深度學習elasticsearch演算法

目錄[-]

1、CRC16 校驗和計算

# 進行crc16 校驗和計算

std::string getCrc16(std::string str){
  std_msgs::UInt8MultiArray data__;
  uint16_t crc = 0xFFFF;
  uint16_t polynomial = 0x1021;
  uint16_t mCrc = 0;
  uint8_t local_data_ = 0;
  uint8_t local_data_sum_ = 0;
  std_msgs::UInt8MultiArray data__;
  for(int i=0;i< str.length();i++){
    if(i==0 || i%2==0){
      local_data_ = char2dec(str[i]);
    }else{
      local_data_sum_ = local_data_ * 16 + char2dec(str[i]);
      local_data_ = 0;
      data__.data.push_back(local_data_sum_);
    }
  }
  for(int i=0; i < data__.data.size(); i++){
    for(int index = 0 ; index < 8; index++){
      mCrc = ((data__.data[i]<<index) & 0x80)^((crc & 0x8000)>>8);
      crc = crc<<1;
      if(mCrc != 0){
        crc^=polynomial;
      }
    }
  }
  std::string temp_ = DecIntToHexStr(crc);
  return temp_;
}