Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

can any one help me to convert below C++ code to c#
stuck with below line:
sOutString.Format(_T("%s %s"), sTimeStamp, sBoardIdentNumber);

Original:
{
  int i, len;
  unsigned char acBuffer[64];
  CString sOutString;
  static char acOut[180];
  sOutString.Format( _T("%s %s"), sTimeStamp, sBoardIdentNumber);
  len = sOutString.GetLength();
 	for( i = 0; i<len; i++)
		acBuffer[i] = sOutString.GetAt(i);
  acBuffer[len] = 0;

  long lCRC = CalculateCRC32( acBuffer, len );
  _snprintf_s(acOut, sizeof(acOut), "%8x", lCRC );

  len = strlen(acOut);
	for( i = 0; i<len; i++)
		acOut[i] = toupper(acOut[i]);

  return acOut;
}


What I have tried:

equivalent C# i tried:
{
      int len;
      byte[] acBuffer = new byte[64];
      string sOutString = null;
      char[] acOut = new char[180];
      //sOutString.Format(_T("%s %s"), sTimeStamp, sBoardIdentNumber);

      len = sOutString.Length;
      for(int i = 0;i < len;i++)
      {
        acBuffer[i] = (byte)sOutString[i];
      }
      acBuffer[len] = 0;

      long lCRC = CalculateCRC32(acBuffer, len);

      len = acOut.Length;
      for (int i = 0; i < len; i++)
      {
        acOut[i] = char.ToUpper(acOut[i]);
      }       

      return acOut;
    }
Posted
Updated 19-Jan-17 2:09am
v2

1 solution

Try:
C#
sOutString = String.Format("{0} {1}", sTimeStamp, sBoardIdentNumber);
 
Share this answer
 
Comments
indyarock 19-Jan-17 4:21am    
yes, it works well.
and i forgot one more line to include.
any idea of c# equivalent for below line?
_snprintf_s(acOut, sizeof(acOut), "%8x", lCRC );
OriginalGriff 19-Jan-17 4:39am    
There isn't one: C# strings do not work on a null terminator.
If what you are trying to do is convert a long to an 8 character hex string all in uppercase, then just use ToString:

long v = 12345678;
string s = v.ToString("X8");

will give you "00BC614E" and eliminate you loop as well.
Do yourself a favour: line by line translations of any language to another do not generally produce good code, no matter how good it was in the original. This is particularly relevant with "old fashioned" C++ to C# because the latter uses the .NET framework which automates a lot of what has to be manual without it.
If you are lifting code from the internet and trying to translate it, be aware that it may not be the best way to do things!
indyarock 31-Jan-17 1:36am    
Thank you :)
it helped

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