1

我似乎无法正确地将以下内容从 VB.NET 转换为 C#--

iKeyChar = Asc(mid(g_Key, i, 1))
iStringChar = Asc(mid(strCryptThis,i,1))

这是我转换后的 C# 代码,它似乎没有输出等效值——

iKeyChar = Convert.ToInt32(g_Key.Substring(i, 1));
iStringChar = Convert.ToInt32(strCryptThis.Substring(i, 1));

任何帮助是极大的赞赏!

4

4 回答 4

8

那是因为Mid是从一开始,而Substring从零开始。试试这种方式:

iKeyChar = (int)Convert.ToChar(g_Key.Substring(i-1, 1));
iStringChar = (int)Convert.ToChar(strCryptThis.Substring(i-1, 1));
于 2012-10-26T21:33:23.477 回答
1

问题在于 ASCII 位。请参阅此处:http ://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/13fec271-9a97-4b71-ab28-4911ff3ecca0和此处:C# 中 VB 的 Asc() 和 Chr() 函数的等价物是什么?

于 2012-10-26T21:37:16.637 回答
0

Simply access the desired char by its index withing the string and cast it to int:

iKeyChar = (int)g_Key[i - 1];
iStringChar = (int)strCryptThis[i - 1];

Note that index of the string characters is zero based (as System Down said in his post).

于 2012-10-26T21:41:02.260 回答
0

尝试这个:

        int iKeyChar = Convert.ToChar(g_key.Substring(i, 1));
        int iStringChar = Convert.ToChar(strCryptThis.Substring(i, 1));
于 2012-10-26T21:36:53.363 回答