Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C C# C#4.0
Hi guys
 
hope someone can convert this piece of code to C# code :
 
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 7-Feb-12 2:39am
Edited 7-Feb-12 2:44am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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..!!!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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):
 
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);
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

#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();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Microsoft Win32 to Microsoft .NET Framework API Map
 
http://msdn.microsoft.com/en-us/library/Aa302340[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

#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();
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

/********************************************************************
This sample enumerates through the tasks on the local computer and
displays their name and state.
********************************************************************/
 
#define _WIN32_DCOM
 
#include
#include
#include
#include
// Include the task header file.
#include
# pragma comment(lib, "taskschd.lib")
# pragma comment(lib, "comsupp.lib")
 

using namespace std;
 
int __cdecl wmain()
{
// ------------------------------------------------------
// Initialize COM.
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if( FAILED(hr) )
{
printf("\nCoInitializeEx failed: %x", hr );
return 1;
}
 
// Set general COM security levels.
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
0,
NULL);
 
if( FAILED(hr) )
{
printf("\nCoInitializeSecurity failed: %x", hr );
CoUninitialize();
return 1;
}
 
// ------------------------------------------------------
// Create an instance of the Task Service.
ITaskService *pService = NULL;
hr = CoCreateInstance( CLSID_TaskScheduler,
NULL,
CLSCTX_INPROC_SERVER,
IID_ITaskService,
(void**)&pService );
if (FAILED(hr))
{
printf("Failed to CoCreate an instance of the TaskService class: %x", hr);
CoUninitialize();
return 1;
}

// Connect to the task service.
hr = pService->Connect(_variant_t(), _variant_t(),
_variant_t(), _variant_t());
if( FAILED(hr) )
{
printf("ITaskService::Connect failed: %x", hr );
pService->Release();
CoUninitialize();
return 1;
}
 
// ------------------------------------------------------
// Get the pointer to the root task folder.
ITaskFolder *pRootFolder = NULL;
hr = pService->GetFolder( _bstr_t( L"\\") , &pRootFolder );

pService->Release();
if( FAILED(hr) )
{
printf("Cannot get Root Folder pointer: %x", hr );
CoUninitialize();
return 1;
}

// -------------------------------------------------------
// Get the registered tasks in the folder.
IRegisteredTaskCollection* pTaskCollection = NULL;
hr = pRootFolder->GetTasks( NULL, &pTaskCollection );
 
pRootFolder->Release();
if( FAILED(hr) )
{
printf("Cannot get the registered tasks.: %x", hr);
CoUninitialize();
return 1;
}
 
LONG numTasks = 0;
hr = pTaskCollection->get_Count(&numTasks);
 
if( numTasks == 0 )
{
printf("\nNo Tasks are currently running" );
pTaskCollection->Release();
CoUninitialize();
return 1;
}
 
printf("\nNumber of Tasks : %d", numTasks );
 
TASK_STATE taskState;

for(LONG i=0; i < numTasks; i++)
{
IRegisteredTask* pRegisteredTask = NULL;
hr = pTaskCollection->get_Item( _variant_t(i+1), &pRegisteredTask );

if( SUCCEEDED(hr) )
{
BSTR taskName = NULL;
hr = pRegisteredTask->get_Name(&taskName);
if( SUCCEEDED(hr) )
{
printf("\nTask Name: %S", taskName);
SysFreeString(taskName);
 
hr = pRegisteredTask->get_State(&taskState);
if (SUCCEEDED (hr) )
printf("\n\tState: %d", taskState);
else
printf("\n\tCannot get the registered task state: %x", hr);
}
else
{
printf("\nCannot get the registered task name: %x", hr);
}
pRegisteredTask->Release();
}
else
{
printf("\nCannot get the registered task item at index=%d: %x", i+1, hr);
}
}
 
pTaskCollection->Release();
CoUninitialize();
return 0;
}
<pre lang="c#"><pre lang="c#"><pre lang="c#">
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 320
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 184
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 10 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100