ianpoley
ianpoley

Reputation: 572

Converting two lines from VB.NET to C#

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

Answers (4)

System Down
System Down

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

Olivier Jacot-Descombes
Olivier Jacot-Descombes

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

Daniel Graham
Daniel Graham

Reputation: 399

Try this:

        int iKeyChar = Convert.ToChar(g_key.Substring(i, 1));
        int iStringChar = Convert.ToChar(strCryptThis.Substring(i, 1));

Upvotes: 0

Related Questions