问题描述
将以下代码转换成C#算法#include<string.h>unsignedintcal_crc(unsignedchar*ptr,unsignedcharlen){unsignedchari;unsignedintcrc=0;while(len--!=0){for(i=0x80;i!=0;i/=2){if((crc&0x8000)!=0){crc*=2;crc^=0x1021;}/*涌CRC乘?郧CRC*/elsecrc*=2;if((*ptr&i)!=0)crc^=0x1021;/*约涌究的RC*/}ptr++;}returncrc;}intmain(intargc,char*argv[]){char*strBytes=NULL;unsignedcharstrTemp[3]={0};intlen=0,i,j,byteLen=0;unsignedintcrc_ret=0;unsignedcharbyteArray[56]={0};if(argc<2)return-1;strBytes=argv[1];len=strlen(strBytes);for(i=0,byteLen;i<len;i+=2){memcpy(strTemp,strBytes+i,2);byteArray[byteLen++]=strtoul(strTemp,NULL,16);}printf("ByteArray[%d]:",byteLen);for(j=0;j<byteLen;j++){printf("%02X",byteArray[j]);}crc_ret=cal_crc(byteArray,byteLen);printf("nCRC:%02X%02Xn",(crc_ret>>8&0xff),(crc_ret&0xff));return0;}
解决方案
解决方案二:
写成COM组件,然后利用DOTNET和COM的互操作方式来调用就好。否则你这个直接翻译成为C#可能回头大,意义不大
解决方案三:
现在已经头大了,本来想写个DLL,但是不会弄啊。而且我系统不支持VC6.0
解决方案四:
static/*这个是静态类需要static*/uintcal_crc(byte[]ptr,bytelen/*这里没有意义byte[]有Length属性*/){bytei;uintcrc=0;for(intj=0;j<ptr.Length;j++){for(i=0x80;i!=0;i/=2){if((crc&0x8000)!=0){crc*=2;crc^=0x1021;}/*涌CRC乘?郧CRC*/elsecrc*=2;if((ptr[j]&i)!=0)crc^=0x1021;/*约涌究的RC*/}}returncrc;}[STAThread]staticintMain(string[]args){stringstrBytes=null;byte[]strTemp=newbyte[3];intlen=0,i,j,byteLen=0;uintcrc_ret=0;byte[]byteArray=newbyte[56];if(args.Length<2)return-1;strBytes=args[1];len=strBytes.Length;for(i=0,byteLen=0;i<len;i+=2){//memcpy(strTemp,strBytes+i,2);byteArray[byteLen++]=Convert.ToByte(strBytes.Substring(i,2),16);//strtoul(strTemp,NULL,16);}Console.Write("ByteArray:"+byteLen);for(j=0;j<byteLen;j++){Console.Write(byteArray[j].ToString("X").PadLeft(2,'0'));}crc_ret=cal_crc(byteArray,(byte)byteLen);Console.Write(string.Format("nCRC:{0}{1}n",(crc_ret>>8&0xff).ToString("X").PadLeft(2,'0'),(crc_ret&0xff).ToString("X").PadLeft(2,'0')));return0;}
这个是按照字面上的代码直接翻译过来的没有测试执行结果不过翻译成C#代码有很多代码已经没有必要了很多可以优化的地方比如上面说的byte[]他已经自带length属性了没有必要需要len参数了main里面也有很多可以很方便的写法自己搞吧
解决方案五:
引用3楼crystal_lz的回复:
static/*这个是静态类需要static*/uintcal_crc(byte[]ptr,bytelen/*这里没有意义byte[]有Length属性*/){bytei;uintcrc=0;for(intj=0;j<ptr.Length;j++){for(i=0x80;i!=0;i/=2){if((crc&0x8000)!=0){crc*=2;crc^=0x1021;}/*涌CRC乘?郧CRC*/elsecrc*=2;if((ptr[j]&i)!=0)crc^=0x1021;/*约涌究的RC*/}}returncrc;}[STAThread]staticintMain(string[]args){stringstrBytes=null;byte[]strTemp=newbyte[3];intlen=0,i,j,byteLen=0;uintcrc_ret=0;byte[]byteArray=newbyte[56];if(args.Length<2)return-1;strBytes=args[1];len=strBytes.Length;for(i=0,byteLen=0;i<len;i+=2){//memcpy(strTemp,strBytes+i,2);byteArray[byteLen++]=Convert.ToByte(strBytes.Substring(i,2),16);//strtoul(strTemp,NULL,16);}Console.Write("ByteArray:"+byteLen);for(j=0;j<byteLen;j++){Console.Write(byteArray[j].ToString("X").PadLeft(2,'0'));}crc_ret=cal_crc(byteArray,(byte)byteLen);Console.Write(string.Format("nCRC:{0}{1}n",(crc_ret>>8&0xff).ToString("X").PadLeft(2,'0'),(crc_ret&0xff).ToString("X").PadLeft(2,'0')));return0;}这个是按照字面上的代码直接翻译过来的没有测试执行结果不过翻译成C#代码有很多代码已经没有必要了很多可以优化的地方比如上面说的byte[]他已经自带length属性了没有必要需要len参数了main里面也有很多可以很方便的写法自己搞吧
感谢,算法还是有错误,不过给我提供了很大帮助。