c# - How can I stop my counter from going to 48 from 0 after it records x for look up in a character array? -


below have included snippet of decrypt method using polybius square. wanting take first 2 user inputs , turn them coordinates letter in square matches input numbers. code works x value , assigns y 48. need finding incriment between x , y without changing y 48.

public string decryptmessage(string userinput)     {         string outputmessage = string.empty;          char[,] alphavalues = new char[5, 5] {             {'a', 'b', 'c', 'd', 'e'},             {'f', 'g', 'h', 'i', 'k'},             {'l', 'm', 'n', 'o', 'p'},             {'q', 'r', 's', 't', 'u'},             {'v', 'w', 'x', 'y', 'z'},         };          char[] userinputarray = userinput.tochararray();              (int = 0; < userinputarray.length; i++)             {                 int x = convert.toint32(userinputarray[i]); i++;             //after point somehow changes 0 48 incriment             //if fixed decrypt work, still trying figure out                  int y = convert.toint32(userinputarray[i]);                   char lettertoreturn = alphavalues[x, y];                   string outputletter = lettertoreturn.tostring();                 outputmessage += outputletter;              }         return outputmessage;     }  

the problem you're converting character representation of digit integer, going numeric value represents character rather digit you're looking for.

try this:

... int x = userinputarray[i++] - '0'; int y = userinputarray[i] - '0'; char lettertoreturn = alphavalues[x, y]; ... 

since '0' through '9' represented 48 through 57, subtracting '0' character turns digit character actual number.

'0' 48, '1' 49, , on way '9' being 57, '1' - '0' 49 - 48 or 1. '9' - '0' 57 - 48 or 9.

of course little trick depends on character set being used, works ascii , unicode.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -