本文介绍了将C代码转换为C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好 希望有人可以将这段代码转换为C#代码: static char s0,s1,s2,s3,s4,s5; // 种子 静态 char byteConv( char byte , int byteNum) { static char b,班次; static char i,c; switch (byteNum) { case 0 :c = byte ^ s0; break ; case 1 :c = byte ^ s1; break ; case 2 :c = byte ^ s2; break ; case 3 :c = byte ^ s3; break ; case 4 :c = byte ^ s4; break ; case 5 :c = byte ^ s5; break ; } shift = 0 ; for (i = 0 ; i < ; 8 ; i ++)shift + =((c>> i)& 0x01); shift =(shift + s0 + s1 + s2 + s3 + s4 + s5)& 0×07; b =(c>> shift)| (c<<( 8 - shift)); 返回 b; } 静态 void responseCode( char challenge0, char challenge1, char challenge2, char challenge3, char challenge4, char challenge5, char * response0, char * response1, char * response2, char * response3 , char * response4, char * response5) { static char b0,b1,b2,b3,b4,b5; b0 = byteConv(challenge0, 0 ); b1 = byteConv(challenge1, 1 ); b2 = byteConv(challenge2, 2 ); b3 = byteConv(challenge3, 3 ); b4 = byteConv(challenge4, 4 ); b5 = byteConv(challenge5, 5 ); * response0 =(b1 + b2 - b3 + b4 - b5)^ b0; * response1 =(b2 + b3 - b4 + b5 - b0)^ b1; * response2 =(b3 + b4 - b5 + b0 - b1)^ b2; * response3 =(b4 + b5 - b0 + b1 - b2)^ b3; * response4 =(b5 + b0 - b1 + b2 - b3)^ b4; * response5 =(b0 + b1 - b2 + b3 - b4)^ b5; } 感谢您的建议。解决方案 添加对于Jochen Arndt的回答,请记住,在C#中,局部变量(其范围在方法中)不能是静态的。您必须将它们从方法中移出到包含类的范围。 然后 static 表示对于该类的多个实例,将只有一个静态变量实例。 如果没有使用C#,我认为应该可以复制代码,将其嵌入到一个类,并删除除第一个以外的所有静态关键字。 你已经问了一个问题对于此代码的部分内容[ ^ ]。根据你到达的答案,通过引用传递它们来替换指针传递的参数。 [/ EDIT] 我会做以下的给定代码: 将数字域从 char 更改为 byte 避免 byte 作为变量名称 更改参数( char * responseX )到输出字节responseX 所有算法都相同(你可能需要转换为字节因为某些运算符可能是为int而不是为byte定义的 将所有运算符组合成一个类 重构名称:使其更像C#,如 重构对象:有种子,挑战,响应数组 BTW: static 可以保留在类中(除了方法中的局部静态变量),尤其是种子可能是静态的 - 意味着,只要程序集存在,值就会存在。或者你删除所有 static ,种子将重置你创建该类的每个新实例。 干杯 Andi Hi guyshope someone can convert this piece of code to C# code :static char s0, s1, s2, s3, s4, s5; // Seedstatic char byteConv (char byte, int byteNum){ static char b, shift; static char i, c; switch (byteNum){ case 0 : c = byte ^ s0; break; case 1 : c = byte ^ s1; break; case 2 : c = byte ^ s2; break; case 3 : c = byte ^ s3; break; case 4 : c = byte ^ s4; break; case 5 : c = byte ^ s5; break;}shift = 0;for (i = 0; i < 8; i++) shift += ((c >> i) & 0x01);shift = (shift + s0 + s1 + s2 + s3 + s4 + s5) & 0x07;b = (c >> shift) | (c << (8 - shift));return b;}static void responseCode (char challenge0, char challenge1, char challenge2, char challenge3, char challenge4, char challenge5, char *response0, char *response1, char *response2, char *response3, char *response4, char *response5){ static char b0, b1, b2, b3, b4, b5; b0 = byteConv (challenge0, 0); b1 = byteConv (challenge1, 1); b2 = byteConv (challenge2, 2); b3 = byteConv (challenge3, 3); b4 = byteConv (challenge4, 4); b5 = byteConv (challenge5, 5); *response0 = (b1 + b2 - b3 + b4 - b5) ^ b0; *response1 = (b2 + b3 - b4 + b5 - b0) ^ b1; *response2 = (b3 + b4 - b5 + b0 - b1) ^ b2; *response3 = (b4 + b5 - b0 + b1 - b2) ^ b3; *response4 = (b5 + b0 - b1 + b2 - b3) ^ b4; *response5 = (b0 + b1 - b2 + b3 - b4) ^ b5;}thanks in advice. 解决方案 Adding to Jochen Arndt''s answer, remember that in C#, local variables (whose scope is within a method) cannot be static. You will have to move them out of their method to the scope of the containing class.Then static means that for multiple instances of that class, there will be only one instance of the static variable.Without ever using C#, I think it should be possible to just copy the code, embed it into a class, and remove all static keywords except the first one.[EDIT]You already asked a question for portions of this code [^]. According to the answers you got there, replace also parameters passed by pointers by passing them by reference.[/EDIT]I would do the following for the given code:change the number domain from char to byteavoid byte as variable namechange out parameters (char* responseX) to out byte responseXall the arithmetic is the same (you might need to cast to byte since some operators might be defined for int but not for byte)put all together into a classrefactor the names: make it more C# likerefactor the objects: have seed, challenge, response arraysBTW: The static can stay within the class (except for the locally static variables in the methods), especially the seed is likely to be static - means, that the values live as long as the assembly lives. Or you remove all static and the seed will reset with each new instance you create of that class.CheersAndi 这篇关于将C代码转换为C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-19 16:47