CRC码的编码和解码程序是啥?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CRC码的编码和解码程序是啥?相关的知识,希望对你有一定的参考价值。

--CRC校验码实验要求:编写出CRC码的编码和解码程序。
(1)实验框架:CRC编码-信道传输-CRC解码。
(2)CRC编码将原始数据按照既定的生成多项式进行编码;
(2)信道传输中实现模拟信道,某些位数据随机发生差错;
(3)CRC解码要能准确判断是否有差错,无差错要恢复出原始数据。

--ARQ协议
实验要求:模拟不同信道环境(6种,三种基本协议,三种滑动窗)下的数据通讯。
(1)实验框架:数据发送程序--模拟信道--数据接收程序
(2)要求理解ARQ协议的内容,分析实验现象

上机实验(3)--获取本地主机名和IP地址
(1)实验要求:通过编程来获取本地主机名和IP地址
(2)学习使用winsock编程,通过WinSock来简单容易的获取IP地址

上机实验(4)--网络命令的应用
(1)实验要求:对ping、tracert、netstat、nslookup、arp、route、nbtstat、net等网络命令进行上机实验。
(2)熟悉各个命令的主要功能,记录实验数据,并进行分析

注:对所有的实验均应提交实验报告,实验报告包括实验目的、实验环境(软、硬件)、实验方法、实验分析和结论等内容。
也可自选题目,但工作量和难度不应低于本大纲。

我有一个别人变得CRC程序,其中有好几种CRC的编码方法,也许会有用
using System;

namespace Communication.IO.Tools

/// <summary>
/// Tool to calculate and add CRC codes to a string
///
/// ***************************************************************************
/// Copyright (c) 2003 Thoraxcentrum, Erasmus MC, The Netherlands.
///
/// Written by Marcel de Wijs with help from a lot of others,
/// especially Stefan Nelwan
///
/// This code is for free. I ported it from several different sources to C#.
///
/// For comments: Marcel_de_Wijs@hotmail.com
/// ***************************************************************************
/// </summary>
public class CRCTool

// 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit
// 'polynom' is the CRC polynom without leading '1' bit
// 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits
// 'crcinit' is the initial CRC value belonging to that algorithm
// 'crcxor' is the final XOR value
// 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not
// 'refout' [0,1] specifies if the CRC will be reflected before XOR
// Data character string
// For CRC-CCITT : order = 16, direct=1, poly=0x1021, CRCinit = 0xFFFF, crcxor=0; refin =0, refout=0
// For CRC16: order = 16, direct=1, poly=0x8005, CRCinit = 0x0, crcxor=0x0; refin =1, refout=1
// For CRC32: order = 32, direct=1, poly=0x4c11db7, CRCinit = 0xFFFFFFFF, crcxor=0xFFFFFFFF; refin =1, refout=1
// Default : CRC-CCITT

private int order = 16;
private ulong polynom = 0x1021;
private int direct = 1;
private ulong crcinit = 0xFFFF;
private ulong crcxor = 0x0;
private int refin = 0;
private int refout = 0;

private ulong crcmask;
private ulong crchighbit;
private ulong crcinit_direct;
private ulong crcinit_nondirect;
private ulong [] crctab = new ulong[256];

// Enumeration used in the init function to specify which CRC algorithm to use
public enum CRCCodeCRC_CCITT, CRC16, CRC32;

public CRCTool()

//
// TODO: Add constructor logic here
//


public void Init(CRCCode CodingType)

switch( CodingType )

case CRCCode.CRC_CCITT:
order = 16; direct=1; polynom=0x1021; crcinit = 0xFFFF; crcxor=0; refin =0; refout=0;
break;
case CRCCode.CRC16:
order = 16; direct=1; polynom=0x8005; crcinit = 0x0; crcxor=0x0; refin =1; refout=1;
break;
case CRCCode.CRC32:
order = 32; direct=1; polynom=0x4c11db7; crcinit = 0xFFFFFFFF; crcxor=0xFFFFFFFF; refin =1; refout=1;
break;


// Initialize all variables for seeding and builing based upon the given coding type
// at first, compute constant bit masks for whole CRC and CRC high bit

crcmask = ((((ulong)1<<(order-1))-1)<<1)|1;
crchighbit = (ulong)1<<(order-1);

// generate lookup table
generate_crc_table();

ulong bit, crc;
int i;
if ( direct == 0 )

crcinit_nondirect = crcinit;
crc = crcinit;
for (i=0; i<order; i++)

bit = crc & crchighbit;
crc<<= 1;
if ( bit != 0 )

crc^= polynom;


crc&= crcmask;
crcinit_direct = crc;

else

crcinit_direct = crcinit;
crc = crcinit;
for (i=0; i<order; i++)

bit = crc & 1;
if (bit != 0)

crc^= polynom;

crc >>= 1;
if (bit != 0)

crc|= crchighbit;


crcinit_nondirect = crc;



/// <summary>
/// 4 ways to calculate the crc checksum. If you have to do a lot of encoding
/// you should use the table functions. Since they use precalculated values, which
/// saves some calculating.
/// </summary>.
public ulong crctablefast (byte[] p)

// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
ulong crc = crcinit_direct;
if ( refin != 0 )

crc = reflect(crc, order);

if ( refin == 0 )

for ( int i = 0; i < p.Length; i++ )

crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ p[i]];


else

for ( int i = 0; i < p.Length; i++ )

crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ p[i]];


if ( (refout^refin) != 0 )

crc = reflect(crc, order);

crc^= crcxor;
crc&= crcmask;
return(crc);


public ulong crctable (byte[] p)

// normal lookup table algorithm with augmented zero bytes.
// only usable with polynom orders of 8, 16, 24 or 32.
ulong crc = crcinit_nondirect;
if ( refin != 0 )

crc = reflect(crc, order);

if ( refin == 0 )

for ( int i = 0; i < p.Length; i++ )

crc = ((crc << 8) | p[i]) ^ crctab[ (crc >> (order-8)) & 0xff ];


else

for ( int i = 0; i < p.Length; i++ )

crc = (ulong)(( (int)(crc >> 8) | (p[i] << (order-8))) ^ (int)crctab[ crc & 0xff ]);


if ( refin == 0 )

for ( int i = 0; i < order/8; i++ )

crc = (crc << 8) ^ crctab[ (crc >> (order-8)) & 0xff];


else

for ( int i = 0; i < order/8; i++ )

crc = (crc >> 8) ^ crctab[crc & 0xff];



if ( (refout^refin) != 0 )

crc = reflect(crc, order);

crc^= crcxor;
crc&= crcmask;

return(crc);


public ulong crcbitbybit(byte[] p)

// bit by bit algorithm with augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
int i;
ulong j, c, bit;
ulong crc = crcinit_nondirect;

for (i=0; i<p.Length; i++)

c = (ulong)p[i];
if ( refin != 0 )

c = reflect(c, 8);


for (j=0x80; j != 0; j>>=1)

bit = crc & crchighbit;
crc<<= 1;
if ( (c & j) != 0)

crc|= 1;

if ( bit != 0 )

crc^= polynom;




for ( i=0; (int)i < order; i++)


bit = crc & crchighbit;
crc<<= 1;
if ( bit != 0 ) crc^= polynom;


if ( refout != 0 )

crc=reflect(crc, order);

crc^= crcxor;
crc&= crcmask;

return(crc);


public ulong crcbitbybitfast(byte[] p)

// fast bit by bit algorithm without augmented zero bytes.
// does not use lookup table, suited for polynom orders between 1...32.
int i;
ulong j, c, bit;
ulong crc = crcinit_direct;

for (i = 0; i < p.Length; i++)

c = (ulong)p[i];
if ( refin != 0)

c = reflect(c, 8);


for ( j = 0x80; j > 0; j >>= 1 )

bit = crc & crchighbit;
crc <<= 1;
if ( (c & j) > 0 ) bit^= crchighbit;
if ( bit > 0 ) crc^= polynom;



if ( refout > 0)

crc=reflect( crc, order );

crc^= crcxor;
crc&= crcmask;

return(crc);


/// <summary>
/// CalcCRCITT is an algorithm found on the web for calculating the CRCITT checksum
/// It is included to demonstrate that although it looks different it is the same
/// routine as the crcbitbybit* functions. But it is optimized and preconfigured for CRCITT.
/// </summary>
public ushort CalcCRCITT(byte[] p)

uint uiCRCITTSum = 0xFFFF;
uint uiByteValue;

for (int iBufferIndex = 0; iBufferIndex < p.Length; iBufferIndex++)

uiByteValue = ( (uint) p[iBufferIndex] << 8);
for ( int iBitIndex = 0; iBitIndex < 8; iBitIndex++ )

if ( ( (uiCRCITTSum^uiByteValue) & 0x8000) != 0 )

uiCRCITTSum = (uiCRCITTSum <<1 ) ^ 0x1021;

else

uiCRCITTSum <<= 1;

uiByteValue <<=1;


return (ushort)uiCRCITTSum;


#region subroutines
private ulong reflect (ulong crc, int bitnum)


// reflects the lower 'bitnum' bits of 'crc'

ulong i, j=1, crcout = 0;

for ( i = (ulong)1 <<(bitnum-1); i != 0; i>>=1)

if ( ( crc & i ) != 0 )

crcout |= j;

j<<= 1;

return (crcout);


private void generate_crc_table()


// make CRC lookup table used by table algorithms

int i, j;
ulong bit, crc;

for (i=0; i<256; i++)

crc=(ulong)i;
if (refin != 0) // 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not

crc=reflect(crc, 8);

crc<<= order-8;

for (j=0; j<8; j++)

bit = crc & crchighbit;
crc<<= 1;
if ( bit !=0 ) crc^= polynom;


if (refin != 0)

crc = reflect(crc, order);

crc&= crcmask;
crctab[i]= crc;


#endregion

参考技术A 你是不是长安学院的学生啊?
我也在找这几个问题的答案哈~~

以上是关于CRC码的编码和解码程序是啥?的主要内容,如果未能解决你的问题,请参考以下文章

python 用于编码/解码莫尔斯码的python代码

我来教你如何将URL进行编码和解码

折线编码/解码中的“级别”到底是啥?

URL安全的Base64编码,解码

kendoui parameterMap 解码 php 对象的正确 json 编码是啥?

python基础学习第四天