求助,将一段C代码转换成C#语言,转换成功愿赠送20元红包

u010594009 2016-01-21 08:30:28
源码如下,转换成功后代码请私信给我


#include <stdio.h>
#include <stdlib.h>
#define SIZEOF_CRC32 4
#define KEY_POLYNOMIAL 0x04C11DB7
unsigned int CalculateCRC32(const unsigned char* pDataStream, unsigned int uiLength, unsigned int lCRCKey)
{
if(pDataStream == NULL)
{
return -1;
};

if ( !uiLength | !lCRCKey)
return -1;

//The CRC processing array (BYTE) comprises of the CRC followed by a data byte
//The data byte is used to store the last byte to be processed by shifting through
//the CRC buffer (like a shift register).
int i, j, iCRC = 0;
char cByte = 0;
int iBytesAdded = 0;
int nCarry = 0;

//bitwise 32 bit modulo long division based on bytes of binary data
//retain remainder only, not the result
for (i = 0; i < ( SIZEOF_CRC32 + uiLength); i++) //correct - flush
{
//Shift in more bytes of the dividend
if ( iBytesAdded < uiLength)
{
cByte = pDataStream[iBytesAdded++];
}
else
{
cByte = 0;
}

//shift the CRC32 for 8 bits, one byte
for (j = 0; j < 8; j++)
{
if( iCRC < 0)
{
nCarry = 1 ;
}
else
{
nCarry = 0;
}

iCRC <<= 1;
//shift dividend polynomial left by one bit
if (cByte < 0) iCRC |= 1;
//add in carry to lsbit
cByte <<= 1;

if (nCarry) iCRC ^= lCRCKey;
}
}
return (unsigned int) iCRC;
}

int main(int argc, char *argv[])
{
char i, count, value[30];
printf("请输入字节回车,00 0A 12 00 01 90,计算结果应为6FF93724:\n");
count=0;
do
{
scanf("%x",&value[count++]);
}
while(getchar()!='\n');

printf("message len is 0x%x\n", count);
unsigned int crc = CalculateCRC32(value, count, KEY_POLYNOMIAL);
printf("crc32 is 0x%x\n", crc);
system("PAUSE");
return 0;
}


...全文
224 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010594009 2016-01-21
  • 打赏
  • 举报
回复
引用 5 楼 ymq_2012 的回复:
这是按照你的写出来的但是结果和你说的不一样
主要看结果,不对呀。 if (nCarry == 0) 好像也错了
  • 打赏
  • 举报
回复

public static int SIZEOF_CRC32 = 4;
        public static int KEY_POLYNOMIAL = 0x04C11DB7;
        public static int CalculateCRC32(char[] pDataStream, int uiLength, int lCRCKey)
        {
            if (pDataStream == null)
            {
                return -1;
            };

            if (uiLength == 0 || lCRCKey == 0)
                return -1;
            int i, j, iCRC = 0;
            char cByte = '0';
            int iBytesAdded = 0;
            int nCarry = 0;

            //bitwise 32 bit modulo long division based on bytes of binary data
            //retain remainder only, not the result
            for (i = 0; i < (SIZEOF_CRC32 + uiLength); i++) //correct - flush
            {
                //Shift in more bytes of the dividend
                if (iBytesAdded < uiLength)
                {
                    cByte = pDataStream[iBytesAdded++];
                }
                else
                {
                    cByte = '0';
                }

                //shift the CRC32 for 8 bits, one byte
                for (j = 0; j < 8; j++)
                {
                    if (iCRC < 0)
                    {
                        nCarry = 1;
                    }
                    else
                    {
                        nCarry = 0;
                    }

                    iCRC <<= 1;
                    //shift dividend polynomial left by one bit
                    if (cByte < 0) iCRC |= 1;
                    //add in carry to lsbit
                    cByte <<= 1;

                    if (nCarry == 0)
                        iCRC ^= lCRCKey;
                }
            }
            return (int)iCRC;
        }
        static void Main(string[] args)
        {
            int count = 0;
            char[] value=new char[30] ;
            Console.WriteLine("请输入字节回车,00 0A 12 00 01 90,计算结果应为6FF93724:\n");
            string s = Console.ReadLine();
            foreach (char c in s.ToCharArray()) {
                value[count++] = c;
            }
            Console.WriteLine("message len is {0}", value.Length);
            int crc = CalculateCRC32(value, count, KEY_POLYNOMIAL);
            Console.WriteLine("crc32 is {0}", crc.ToString("x8"));
            Console.ReadLine();
        }
这是按照你的写出来的但是结果和你说的不一样
u010594009 2016-01-21
  • 打赏
  • 举报
回复
引用 3 楼 LvBao_117 的回复:
自己转换呗,unsigned int CalculateCRC32(const unsigned char* pDataStream, unsigned int uiLength, unsigned int lCRCKey) { 就是:public uint CalculateCRC32(const uint ref char pDataStream, uint uiLength, uint lCRCKey) {
自己搞不定,char* value 不知道是啥情况,输入16进制的90,结果scanf("%x",&value[count++]) 后变成了-112,不是144
LvBao_117 2016-01-21
  • 打赏
  • 举报
回复
自己转换呗,unsigned int CalculateCRC32(const unsigned char* pDataStream, unsigned int uiLength, unsigned int lCRCKey) { 就是:public uint CalculateCRC32(const uint ref char pDataStream, uint uiLength, uint lCRCKey) {
u010594009 2016-01-21
  • 打赏
  • 举报
回复
引用 1 楼 diaodiaop 的回复:
如果不是 你也不要着急 直接搜c# crc32校验就可以搜索到相关信息,.
谢谢你的及时回复。 对方是用此段代码生成校验码的,我也只能用同样的算法生成来比对,但是对c不熟,始终搞不定
by_封爱 版主 2016-01-21
  • 打赏
  • 举报
回复
CalculateCRC32校验... 我不清楚你那是什么设备 我这里有一个校验 你看看是不是你想要的...

public static byte[] crc(byte[] src)
        {
            int len = src.Length;
            UInt16 CRC = 0xffff;
            UInt16 next;
            bool carry;
            UInt16 n;

            int i = 0;
            while (len-- > 0)
            {
                next = (UInt16)src[i];
                CRC ^= next;
                for (n = 0; n < 8; n++)
                {
                    carry = (CRC & 1) != 0;
                    CRC >>= 1;
                    if (carry)
                    {
                        CRC ^= 0xA001;
                    }
                }
                i++;
            }
            var a = BitConverter.GetBytes(CRC);
            return a;
        }

如果不是 你也不要着急 直接搜c# crc32校验就可以搜索到相关信息,.
本拉灯 2016-01-21
  • 打赏
  • 举报
回复
引用 4 楼 u010594009 的回复:
[quote=引用 3 楼 LvBao_117 的回复:] 自己转换呗,unsigned int CalculateCRC32(const unsigned char* pDataStream, unsigned int uiLength, unsigned int lCRCKey) { 就是:public uint CalculateCRC32(const uint ref char pDataStream, uint uiLength, uint lCRCKey) {
自己搞不定,char* value 不知道是啥情况,输入16进制的90,结果scanf("%x",&value[count++]) 后变成了-112,不是144[/quote] -112,不是144 一个是有符号一个是无符号 的 你把-112&0xFF就是144了

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧