程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c#-請教如何將如下VC++代碼轉換成C#代碼,謝謝

c#-請教如何將如下VC++代碼轉換成C#代碼,謝謝

編輯:編程解疑
請教如何將如下VC++代碼轉換成C#代碼,謝謝
int nKeyLen = strKey.GetLength();
if (nKeyLen == 0)
    strKey = _T("xxsswww");
srand((unsigned)time(NULL));
int nRange = 256;
int nOffset = (rand() % nRange);
CString strDest = _T("");
strDest.Format(_T("%1.2x"), nOffset);

int nSrcAsc = 0;
int nKeyPos = 0;
for (int nSrcPos = 0; nSrcPos < strSrc.GetLength(); nSrcPos++)
{
    int n = strSrc[nSrcPos];
    nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
    if (nKeyPos < nKeyLen - 1)
        nKeyPos += 1;
    else
        nKeyPos = 1;
    nSrcAsc = nSrcAsc ^ strKey[nKeyPos];
    CString strTemp = _T("");
    strTemp.Format(_T("%1.2x"), nSrcAsc);
    strDest += strTemp;
    nOffset = nSrcAsc;
}
return strDest;

最佳回答:


 int nKeyLen = strKey.Length;
if (nKeyLen == 0)
    strKey = "xxsswww;
Random rnd = new Random();
int nRange = 256;
int nOffset = (rnd.NextInt() % nRange);
string strDest = string.Format("{0:X2}", nOffset);
int nSrcAsc = 0;
int nKeyPos = 0;
for (int nSrcPos = 0; nSrcPos < strSrc.Length; nSrcPos++)
{
    int n = strSrc[nSrcPos];
    nSrcAsc = (strSrc[nSrcPos] + nOffset) % 255;
    if (nKeyPos < nKeyLen - 1)
        nKeyPos += 1;
    else
        nKeyPos = 1;
    nSrcAsc = nSrcAsc ^ strKey[nKeyPos];
    string strTemp = "";
    strTemp = string.Format("{0:X2}", nSrcAsc);
    strDest += strTemp;
    nOffset = nSrcAsc;
}
return strDest;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved