0

I have strings which are in a format below: "p100" "p231" "p000" . . .

these strings are button names and the last 3 characters are referring to a cell of a 3D array,and I need to convert those strings to integers to get the cell address, I use this:

string str = clickedButton.Name.ToString();
int xi ;
int xj;
int xz;
xi = Convert.ToInt32(str[1]);
xj = Convert.ToInt32(str[2]);
xz = Convert.ToInt32(str[3]);

please note that I don't use "str[0]" because it is "p".

but when I compile my code , the value of xi,xj,xz are the Ascci values of the string characters.

how should I convert string to int so that it won't happen?

esmaily
  • 3
  • 4

3 Answers3

0

Do the following:

int xi = (int)Char.GetNumericValue(str[1]);

Also refer to this

Community
  • 1
  • 1
Rana
  • 1,675
  • 3
  • 25
  • 51
0

Your code is going after the character at the position.

What you want is to get the string at the position.

xi = Convert.ToInt32(str.Substring(1,1));
xj = Convert.ToInt32(str.Substring(2,1));
xk = Convert.ToInt32(str.Substring(3,1));
Dave Bush
  • 2,382
  • 15
  • 12
0

Why not just format the string to remove p from the name directly?

string str = clickedButton.Name.ToString();
str = str.Replace("p","");
int a = Convert.ToInt32(str);
int onesplace = a%10;
int secondplace = (a/10)%10;
int thirdplace = a/100;

The 'places' start from right to left. Ones place is the right most digit

If you want the individual values for the cell, you can then convert this string into an integer and use the modulus function

EDIT: updated code for it

Haardik
  • 185
  • 2
  • 6