Reputation: 572
I can't seem to properly convert the following from VB.NET to C#--
iKeyChar = Asc(mid(g_Key, i, 1))
iStringChar = Asc(mid(strCryptThis,i,1))
Here's my converted C# code, which doesn't seem to output equivalent values--
iKeyChar = Convert.ToInt32(g_Key.Substring(i, 1));
iStringChar = Convert.ToInt32(strCryptThis.Substring(i, 1));
Any help is greatly appreciated!
Upvotes: 1
Views: 247
Reputation: 6260
That's because Mid
is one based while Substring
is zero based. Try it this way:
iKeyChar = (int)Convert.ToChar(g_Key.Substring(i-1, 1));
iStringChar = (int)Convert.ToChar(strCryptThis.Substring(i-1, 1));
Upvotes: 8
Reputation: 112279
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).
Upvotes: 0
Reputation: 6781
It's the ASCII bit that's the problem. See here: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/13fec271-9a97-4b71-ab28-4911ff3ecca0 and here: What's the equivalent of VB's Asc() and Chr() functions in C#?
Upvotes: 1
Reputation: 399
Try this:
int iKeyChar = Convert.ToChar(g_key.Substring(i, 1));
int iStringChar = Convert.ToChar(strCryptThis.Substring(i, 1));
Upvotes: 0