CRC算法模拟程序,求解

CRC算法模拟程序,求解,第1张

using System

using System.Collections.Generic

using System.Linq

using System.Text

using System.IO

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

string m, p

int i, j, count, M = 0, P = 0

int temp1=0, temp2, temp3

using (StreamReader input_m = new StreamReader("input_m.txt", Encoding.UTF8))

{

m = input_m.ReadLine()

}

using (StreamReader input_p = new StreamReader("input_p.txt", Encoding.UTF8))

{

p = input_p.ReadLine()

}

using (StreamWriter sw = new StreamWriter("output.txt", false, Encoding.UTF8))

{

for (i = 0i <p.Lengthi++)

{

M *= 10

M += (m[i] - '0')

temp1 *= 10

temp1 += (p[i] - '0')

}

P = temp1

temp2 = M

for (j = 0j <= m.Length - p.Lengthj++)

{

if ((M - 1) * 10 >P)

{

P = temp1

for (M = 0, count = 0count <4count++)

{

temp3 = ((int)(temp2 / Math.Pow(10, 3 - count))) % 10

M += (temp3 + (p[count] - '0')) == 1 ? 1 : 0

M *= 10

}

if (j + p.Length >4 &&j + p.Length <m.Length)

{

M += (m[j + p.Length] - '0')

}

M = M % (int)(Math.Pow(10, p.Length))

M = M * 10 + (m[i + 1] - '0')

M /= 10

temp2 = M

}

else

{

P = 0

M = (M - P) * 10 + (m[i + 2] - '0')

temp2 = M

}

}

for (i = 0i++)

{

if (((M / 10) / (int)Math.Pow(10, i)) == 0)

break

}

for (j = 0j <p.Length - 1 - ij++ )

sw.Write('0')

sw.Write(M/10)

}

}

}

}

求积分吖...

LZ是ZJTCM的? DILL的作业?

我有一个别人变得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: [email protected]

/// ***************************************************************************

/// </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=0refin =0, refout=0

// For CRC16: order = 16, direct=1, poly=0x8005, CRCinit = 0x0, crcxor=0x0refin =1, refout=1

// For CRC32: order = 32, direct=1, poly=0x4c11db7, CRCinit = 0xFFFFFFFF, crcxor=0xFFFFFFFFrefin =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 CRCCode{CRC_CCITT, CRC16, CRC32}

public CRCTool()

{

//

// TODO: Add constructor logic here

//

}

public void Init(CRCCode CodingType)

{

switch( CodingType )

{

case CRCCode.CRC_CCITT:

order = 16direct=1polynom=0x1021crcinit = 0xFFFFcrcxor=0refin =0refout=0

break

case CRCCode.CRC16:

order = 16direct=1polynom=0x8005crcinit = 0x0crcxor=0x0refin =1refout=1

break

case CRCCode.CRC32:

order = 32direct=1polynom=0x4c11db7crcinit = 0xFFFFFFFFcrcxor=0xFFFFFFFFrefin =1refout=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=0i<orderi++)

{

bit = crc &crchighbit

crc<<= 1

if ( bit != 0 )

{

crc^= polynom

}

}

crc&= crcmask

crcinit_direct = crc

}

else

{

crcinit_direct = crcinit

crc = crcinit

for (i=0i<orderi++)

{

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 = 0i <p.Lengthi++ )

{

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

}

}

else

{

for ( int i = 0i <p.Lengthi++ )

{

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 = 0i <p.Lengthi++ )

{

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

}

}

else

{

for ( int i = 0i <p.Lengthi++ )

{

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

}

}

if ( refin == 0 )

{

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

{

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

}

}

else

{

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

{

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=0i<p.Lengthi++)

{

c = (ulong)p[i]

if ( refin != 0 )

{

c = reflect(c, 8)

}

for (j=0x80j != 0j>>=1)

{

bit = crc &crchighbit

crc<<= 1

if ( (c &j) != 0)

{

crc|= 1

}

if ( bit != 0 )

{

crc^= polynom

}

}

}

for ( i=0(int)i <orderi++)

{

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 = 0i <p.Lengthi++)

{

c = (ulong)p[i]

if ( refin != 0)

{

c = reflect(c, 8)

}

for ( j = 0x80j >0j >>= 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 = 0iBufferIndex <p.LengthiBufferIndex++)

{

uiByteValue = ( (uint) p[iBufferIndex] <<8)

for ( int iBitIndex = 0iBitIndex <8iBitIndex++ )

{

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 != 0i>>=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=0i<256i++)

{

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=0j<8j++)

{

bit = crc &crchighbit

crc<<= 1

if ( bit !=0 ) crc^= polynom

}

if (refin != 0)

{

crc = reflect(crc, order)

}

crc&= crcmask

crctab[i]= crc

}

}

#endregion

}

}

#include <stdio.h>

#include <string.h>

#include "stdlib.h"

unsigned int char2int(char *str)

{

unsigned int count=0, ret=0

for(count = 0count<strlen(str)count++)

{

ret = ret<<1

if('0' != str[count])

{ ret+=1}

}

return ret

}

unsigned int getR(char *str)

{

unsigned int c =0

int ret = strlen(str)-1

for(c=0c <strlen(str)c++)

{if(str[c] != '0')<br/> {return ret-c}

}

}

int getRi(unsigned int num)

{

int c =0

for(num != 0c++)

{num = num>>1}

return c

}

void CRC(char *scode, char *p, char*g )

{

unsigned int iP = char2int(p)

unsigned int iG = char2int(g)

unsigned int r= getR(g)

unsigned int code = iP <<r

unsigned int yx = code

for(getRi(yx) >= getRi(iG))

{ yx = yx ^ (iG<<(getRi(yx) - getRi(iG)))}

code += yx

itoa(code,scode,2)

}

void main() //定义主函数

{

char data[8]="" , bds[8]="",code[16]=""

printf("数据:")

scanf("%s", data)

printf("表达式:")

scanf("%s", bds)

CRC(code,data,bds)

printf("编码:%s",code)

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12205670.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存