Click here to Skip to main content
15,311,561 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
See more:
Hi guys

hope someone can convert this piece of code to C# code :

C#
static char s0, s1, s2, s3, s4, s5; // Seed
static 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.
Posted
Updated 22-Aug-21 21:08pm
v2

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]
   
v2
I would do the following for the given code:

  • change the number domain from char to byte
  • avoid byte as variable name
  • change out parameters (char* responseX) to out byte responseX
  • all 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 class
  • refactor the names: make it more C# like
  • refactor the objects: have seed, challenge, response arrays


BTW: 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.

Cheers

Andi
   
v2
simple,why dont you apply the same logic using C#...???
it is the best way to convert your code from one language to another...best of luck..!!!
   
char in C++ is equivalent to byte or sbyte in C#.

When bit shifting etc in C#, the result is an int so a cast back to byte is required.

Assuming the seed values are assigned to elsewhere and the C++ code isn't relying on some default overflow behavior not present in .net then the code below is a straight conversion (you may also want to decide what the default case in the switch should be):

C#
public static byte s0, s1, s2, s3, s4, s5; // Seed

    public static byte byteConv(byte byt, int byteNum)
    {
        byte b, shift, c;
        switch (byteNum)
        {
            default:
            case 0:
                c = (byte)(byt ^ s0);
                break;
            case 1:
                c = (byte)(byt ^ s1);
                break;
            case 2:
                c = (byte)(byt ^ s2);
                break;
            case 3:
                c = (byte)(byt ^ s3);
                break;
            case 4:
                c = (byte)(byt ^ s4);
                break;
            case 5:
                c = (byte)(byt ^ s5);
                break;
        }
        shift = 0;
        for (byte i = 0; i < 8; i++)
            shift += (byte)((c >> i) & 0x01);
        shift = (byte)((shift + s0 + s1 + s2 + s3 + s4 + s5) & 0x07);
        b = (byte)((c >> shift) | (c << (8 - shift)));
        return b;
    }

    public static void responseCode(
        byte challenge0,
        byte challenge1,
        byte challenge2,
        byte challenge3,
        byte challenge4,
        byte challenge5,
        out byte response0,
        out byte response1,
        out byte response2,
        out byte response3,
        out byte response4,
        out byte response5)
    {
        byte 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 = (byte)((b1 + b2 - b3 + b4 - b5) ^ b0);
        response1 = (byte)((b2 + b3 - b4 + b5 - b0) ^ b1);
        response2 = (byte)((b3 + b4 - b5 + b0 - b1) ^ b2);
        response3 = (byte)((b4 + b5 - b0 + b1 - b2) ^ b3);
        response4 = (byte)((b5 + b0 - b1 + b2 - b3) ^ b4);
        response5 = (byte)((b0 + b1 - b2 + b3 - b4) ^ b5);
    }
   
Microsoft Win32 to Microsoft .NET Framework API Map

http://msdn.microsoft.com/en-us/library/Aa302340[^]
   
C#
#include<stdio.h>
#include<conio.h>
void main()
{
 char s[10][10];
 int i,j;
 clrscr();
 for(i=0;i<9;i++)
 {
   for(j=0;j<9;j++)
   {
   s[i][j]='*';
   }
 }
 for(i=0;i<4;i++)
 {
   for(j=1;j<4;j++)
   {
   s[i][j]=' ';
   }
 }
 for(i=1;i<4;i++)
 {
   for(j=5;j<9;j++)
   {
   s[i][j]=' ';
   }
 }
 for(i=5;i<8;i++)
 {
   for(j=0;j<4;j++)
   {
   s[i][j]=' ';
   }
 }
 for(i=5;i<9;i++)
 {
   for(j=5;j<8;j++)
   {
   s[i][j]=' ';
   }
 }
 for(i=0;i<9;i++)
 {
   for(j=0;j<9;j++)
   {
     printf("%c  ",s[i][j]);
   }
   printf("\n");
 }
 getch();
}
   
C#
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int i=0,k,n,n1,n2,n3,n4,count=0,len=0;
char s1[40],s2[40],s3[40],s4[6]={'F','L','A','M','E','S'};
clrscr();
gets(s1);
gets(s2);
n1=strlen(s1);
n2=strlen(s2);
for(i=0;i<n1;i++)
{
for(i=0;i<n2;i++)
{
if(s1[i]==s2[i])
 {
n1--;
n2--;
  }
 }
}
n3=strchar(s1,s2);
n4=strlen(s4);
count=n4+n3;
{
for(k=1;k<n4;k++)
{
if(count==s4[k])
{
len--;
k++;
}
}
getch();
}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900