Data Type Conversion
Hey can any one tell me please...
i input 4 digits in the input buffer... (1234) and read them as string
myString = Console.ReadLine();
then i wasnt to split those number into individual digits.
int=input=0;char c;
for (int i = 0; i < myString.Length; i++)
{
c = myString[i];
input = Convert.ToInt32(c);
Console.WriteLine(input);
}
after the compilation of the program and user input i get this output
49
50
51
52
where as i think it should be something like
1
2
3
4
so my problem is that...Convert.ToInt32(c); is not converting...and i am not getting error as well.
and when i change this type conversion line of code to
input = Int32.Parse(c);
then i get the following error
CODE
Error 1 The best overloaded method match for 'int.Parse(string)' has some invalid arguments D:\My .NET Work\C #\Console\Dietel C Sharp\Chapter 4\Exercise 4.15\Exercise 4.15\Program.cs 30 25 Exercise 4.15
Error 2 Argument '1': cannot convert from 'char' to 'string' D:\My .NET Work\C #\Console\Dietel C Sharp\Chapter 4\Exercise 4.15\Exercise 4.15\Program.cs 30 37 Exercise 4.15
please help me solve this tiny problem...thank you.
RentedLips
§ Rented Lips §
----------------
Begning To Learn
----------------
AlanPosted Jun 24, 2007, 5:08 AM
For example the digits 0 to 9 correspond to the unicode values 48 to 57 and this is why, when converting the string "1234", you've ended up with 49 50 51 52.
If you do it like this, you'll get the desired result:
string myString = Console.ReadLine();
char c;
for (int i = 0; i < myString.Length; i++)
{
c = myString[i];
Console.WriteLine(c);
}