C语言转换成C#代码

问题描述

将以下代码转换成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里面也有很多可以很方便的写法自己搞吧

感谢,算法还是有错误,不过给我提供了很大帮助。

时间: 2024-09-17 04:06:49

C语言转换成C#代码的相关文章

c语言-C语言转换成大写,结果为什么不对?哪里错了

问题描述 C语言转换成大写,结果为什么不对?哪里错了 #include#includemain(){ char aa[]=""Hello world""; int cd=strlen(aa); int i; for(i=0;i<cd;i++) if (97<=aa[i]<=122)aa[i]-=32; printf(""%s""aa);} 解决方案 (97<=aa[i]<=122)这个不对(97&

java源码-求这个题目用c语言换成java代码

问题描述 求这个题目用c语言换成java代码 ?????????????????????????????????????????????????????????????????? 解决方案 int n, u, d; Scanner in = new Scanner(System.in); while (true) { n = in.nextInt(); u = in.nextInt(); d = in.nextInt(); if (n == 0) break; int t = (n - u)/(

求助c语言高手帮在下解决几个棘手的问题, 就是帮忙把fortran语言转换成c语言

问题描述 求助c语言高手帮在下解决几个棘手的问题, 就是帮忙把fortran语言转换成c语言 READ(*,*)X0,EP N=1 10 X=1.0/(X0*X0+1.0) WRITE(*,200) N,X 200 FORMAT(1X,2HN=,I4,5X,2HX=,F10.6) IF (ABS(X-X0).LT.EP) GOTO 20 X0=X N=N+1 GOTO 10 20 STOP END

php ubb转换成html,html转换成ubb代码

php ubb转换成html,html转换成ubb代码 function htmlToUBB($str) {  $str = preg_replace("/<img[^>]+src="([^"]+)"[^>]*>/i","[img]$1[/img]",$str);  $str = preg_replace("/<embed[^>]+src="([^"]+)"[^

html转换成UBB代码

html转换成UBB代码 Function Html2Ubb(str)  If Str<>"" And Not IsNull(Str) Then   Dim re,tmpstr   Set re=new RegExp   re.IgnoreCase =True   re.Global=True   re.Pattern = "(<STRONG>)":Str = re.Replace(Str,"<b>")   r

帮忙将下面的php语言转换成c#

问题描述 帮忙将下面的php语言转换成c# 下面这些转换成C#,该怎么转? <?php class Application_Model_Checknote{ private $db; private $incheck='in_check'; private $outcheck='out_check'; private $table=array('check_office'=>'patient_check_office','out_check'=>'patient_out_check',

asp.net中文简体转换成繁体代码

这是一款简单的asp教程.net教程中文简体转换成繁体代码哦,这款中文简单转换繁体的简单代码,挨过来繁体转换成简体也一样. //Form1.Designer.cs namespace TestEnCode { partial class Form1 { /// <summary> /// 必需的设计器变量. /// </summary> private System.ComponentModel.IContainer components = null; /// <summa

把c++语言转换成c语言-如何把C++语言编写的代码转换成C语言

问题描述 如何把C++语言编写的代码转换成C语言 #include <stdlib.h>#include <stdio.h> #include <string.h>#include <conio.h>#include <iostream.h>#define N 50 //系统中学生的最大数量int n=3; //学生数量的初始值struct student { int sid; char name[10]; char num[14]; float

IBM RSA (IBM rational software architect ) V8 学习之四 C++类图转换成c++代码

原文http://blog.csdn.net/ztz0223/article/details/7596025   前一讲: RSA建模 c++类图 已经说过了,如何生成一个结构体,添加成员变量,添加带参数的成员函数,那么如何看到效果呢? 这就是本文要说的了:变换->UML to C++ 回忆一下,之前建立的模型--c++类图.   现在要用这个类图类生成相应的代码: 步骤一,选择建模文件example_uml,右键,弹出窗口选择变换->新建配置:   步骤二,如图输入配置名:   步骤三,创建