Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,
i want to use each digit in textbox.for example if input in textbox is 123456
i want to take firstdigit (here is 1) and do something wih it.
i converted the input to charArray and used each one,but it shows 48 instead of first element(here is 1) and 49 for second element.
what should i do?thanks
Posted

You could try
foreach (char a in TextBox1.Text)
    {
       int i = System.Convert.ToInt32(System.Char.ConvertFromUtf32(a));
    }
 
Share this answer
 
Comments
Ed Nutting 5-Jun-11 6:24am    
Much simpler - no idea what OriginalGriff's would come out with - my 5+ :)

Edit: Okay I now see what it would come out with, but it seems very complex compared to your solution ;P
Abhinav S 5-Jun-11 8:01am    
If OriginalGriff has come up with an answer, in all likelihood it will be better than mine.

Thanks for the 5. :)
Don't worry about it. It is just a different way of showing the character value: '1' is equal to 49, or 31 in Hex.

[edit]
Thinking about it, you probably want to use the character converted to an int or similar:
C#
string s = "12345";
char[] ac = s.ToCharArray();
foreach (char c in ac)
    {
    int i = (int) char.GetNumericValue(c);
    Console.WriteLine(i);
    }
You can convert the whole original string with
int i = int.Parse(s);

[/edit]



"arashmobileboy - 18 mins ago
sorry,now how can i put them into an array?
Reply
OriginalGriff - 10 mins ago
What kind of array do you want to use? What are you actually trying to achieve? Because I can't help but think you are going in a odd direction...
Reply
arashmobileboy - 6 mins ago
i want to make an array of input numbers in textbox,and for example do i[0]+15"


OK - I don't know why you want to do that, but...
string s = "12345";
char[] ac = s.ToCharArray();
int[] digits = new int[ac.Length];
for (int i = 0; i < ac.Length; i++)
    {
    digits[i] = (int) char.GetNumericValue(ac[i]);
    }
Console.WriteLine(digits[0] + 15);
 
Share this answer
 
v3
Comments
arashmobileboy 5-Jun-11 5:04am    
thanks,but the problem is when i do char[0]*10 in does 49*10=490 but i want to do 1*10(according to above example)
OriginalGriff 5-Jun-11 5:08am    
See my modified answer! I thought of that while you replied...:laugh:
Kim Togo 5-Jun-11 5:10am    
Good answer Griff. My 5.
Manfred Rudolf Bihy 5-Jun-11 5:13am    
My 5!
"I thought of that while you replied ..."
So you're into that mind reading bit once again, eh? ;)
OriginalGriff 5-Jun-11 5:18am    
I take off the tin-foil hat for thirty seconds...:laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900