CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2011
    Posts
    4

    Help Me Convert Some Code

    Hello,

    I have a routine that is currently in c++ and I really need to have it converted to c sharp. Could someone with a lot more knowledge then myself have a look at the code and help me out.

    The code follows:

    Thanks

    Mike

    This is the c++ code.

    Code:
    char * computeChecksum(char*  message)
            {
                static char ascii_checksum[ 5 ];
                WORD checksum     = 0
                int i                          = 0;
    
                while ( message[i] != '\0';
                {
                   checksum += (unsigned) message[i];
                   i++ ;
                }
    
                checksum =    -(checksum & 0xFFFF);
                sprintf( ascii_checksum, "%4.4X, checksum );
          
                return ascii_checksum;
    
            }

  2. #2
    Join Date
    Sep 2011
    Posts
    6

    Re: Help Me Convert Some Code

    string computeChecksum(string m)
    {
    char[] message=m.ToCharArray();
    static char ascii_checksum[ 5 ];
    int checksum = 0
    int i = 0;

    while ( message[i] != '\0';
    {
    checksum += (int) message[i];
    i++ ;
    }

    checksum = -(checksum & 0xFFFF);
    string ascii_checksum= checksum.ToString() ;

    return ascii_checksum;

    }
    i guess this would help you

  3. #3
    Join Date
    Aug 2005
    Posts
    198

    Re: Help Me Convert Some Code

    Your C++ code was not compilable - after correcting it and converting I get:
    Code:
    private string computeChecksum_ascii_checksum = new string(new char[5]);
    private string[] computeChecksum(sbyte[] message)
    {
    //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
    //	static sbyte ascii_checksum[5];
    	ushort checksum = 0;
    	int i = 0;
    
    	while (message[i] != '\0')
    	{
    	   checksum += (uint) message[i];
    	   i++;
    	}
    
    	checksum = -(checksum & 0xFFFF);
    	computeChecksum_ascii_checksum = string.Format("{0,4:X4}", checksum);
    
    	return computeChecksum_ascii_checksum;
    }
    Last edited by David Anton; September 8th, 2011 at 05:53 PM.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  4. #4
    Join Date
    Sep 2011
    Posts
    6

    Re: Help Me Convert Some Code

    Quote Originally Posted by David Anton View Post
    Your C++ code was not compilable - after correcting it and converting I get:
    Code:
    private string computeChecksum_ascii_checksum = new string(new char[5]);
    private string[] computeChecksum(sbyte[] message)
    {
    //C++ TO C# CONVERTER NOTE: This static local variable declaration (not allowed in C#) has been moved just prior to the method:
    //	static sbyte ascii_checksum[5];
    	ushort checksum = 0;
    	int i = 0;
    
    	while (message[i] != '\0')
    	{
    	   checksum += (uint) message[i];
    	   i++;
    	}
    
    	checksum = -(checksum & 0xFFFF);
    	computeChecksum_ascii_checksum = string.Format("{0,4:X4}", checksum);
    
    	return computeChecksum_ascii_checksum;
    }
    Method should return String not String[]

  5. #5
    Join Date
    Aug 2005
    Posts
    198

    Re: Help Me Convert Some Code

    Right you are (this is fixed in the next build).
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured