首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

请问达人CRC16非标准函数

2012-08-09 
请教达人CRC16非标准函数C# codeprivate static ushort xcrc(ushort crc, byte cp){//return ((crc)8)^c

请教达人CRC16非标准函数

C# code
    private static ushort xcrc(ushort crc, byte cp)    {        //return ((crc)>>8)^crctab[((crc)^(ushort)(cp))&0xff];        ushort t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0;        t1 = (ushort)(crc >> 8);        t2 = (ushort)cp;        t3 = (ushort)(crc ^ t2);        t4 = (ushort)(t3 & 0xFF);        t5 = (ushort)(t1^crctab[t4]);        return t5;    }    /// <summary>    /// 添加CRC校验字    /// </summary>    /// <param name="bufin">信息串</param>    /// <param name="n">不包括校验字的串总长度</param>    public void ConCRC(ref byte[] bufin, byte n)    {        ushort crc16 = 0;        byte i;        //n个数据的CRC校验        for (i = 0; i < n; i++)        {            crc16 = xcrc(crc16, bufin[i]);        }        ////高字节在前,低字节在后        //bufin[i] = (byte)(crc16 >> 8);        //bufin[i + 1] = (byte)(crc16 & 0xff);        //因为BX-4K2通讯协议 低字节在前 高字节在后        bufin[i+1] = (byte)(crc16 >> 8);        bufin[i] = (byte)(crc16 & 0xff);    }查表的CRC16校验,这是我们根据他们给的写的C#的程序,BCB调整不好了,如下[code=Delphi(Pascal)] unsigned short xcrc(unsigned short crc,byte cp)   {        unsigned short t1=0,t2=0,t3=03,t4=0,t5=0;        t1=(unsigned short)(crc>>8);        t2=(unsigned short)cp;        t3=(unsigned short)(crc^t2);        t4=(unsigned short)(t3&0xFF);        t5=(unsigned short)(t1^tabel[t4]);        return t5;   }   unsigned short CRC_16(byte& bufin[], byte n)    {        byte i;        unsigned short nAccum = 0;        for (i = 0; i < n;i++ )            {                nAccum=xcrc(nAccum,bufin[i]);            }    }

[/code]

[解决办法]
C/C++ code
// tabel 是 unsigned shor tabel[] ?typedef unsigned char byte;unsigned short xcrc(unsigned short crc, byte cp){    unsigned short t1=0,t2=0,t3=03,t4=0,t5=0;    t1=(unsigned short)(crc>>8);    t2=(unsigned short)cp;    t3=(unsigned short)(crc^t2);    t4=(unsigned short)(t3&0xFF);    t5=(unsigned short)(t1^tabel[t4]);    return t5;}unsigned short CRC_16(byte *bufin, byte n){    byte i;    unsigned short nAccum = 0;        for (i = 0; i < n;i++ )    {        nAccum=xcrc(nAccum,bufin[i]);    }} 

热点排行