Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Type Conversion

Status
Not open for further replies.

hisheeraz

Programmer
Mar 24, 2003
28
0
0
AU
Hey can any one tell me please...

i input 4 digits in the input buffer... (1234) and read them as string
Code:
myString = Console.ReadLine();
then i wasnt to split those number into individual digits.
Code:
            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
Code:
49
50
51
52
where as i think it should be something like
Code:
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
Code:
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
----------------
 
the problem is that Int32.Parse() method only accepts String as its parameter. so you should convert Char to String before calling this method. Try the code below:

Code:
String myString = "1234";
 int input;
 char c;
            for (int i = 0; i < myString.Length; i++)
            {
                c = myString[i];
                String s = c.ToString();
                input = Int32.Parse(s);
                Console.WriteLine(input);
            }

you can also use Convert.ToInt32() method instead of Int32.Parse(). the output 49,50... which you got is the ASCII codes of characters 1,2...

Touraj Ebrahimi
toraj_e@yahoo.com
 
i have an alternative solution for you but sneaky, try the code below:

Code:
string s = "1234";
int input;
    foreach (char c in s)
    {
     Console.WriteLine(c)
    }

you see that by the means of this solution you don't need conversion but be carefull if you want do arithmatic operation on c the output will vary and you should convert to int before it.

Touraj Ebrahimi
toraj_e@yahoo.com
 
so my problem is that...Convert.ToInt32(c); is not converting...
It is converting the Char to int but your problem is that a char of 1 has an asci value of 49. see the link below then look up Chr value till you find 1, move across to the decimal column (Dec) and you will see the value 49. Hence the reason that Parse takes a string (not every everyone knows that a char has an int value). BTW you should use Int.TryParse() (see code below)
Code:
int result = 0;
if (!Int32.TryParse(myString, out result))
{
  MessageBox.Show("Could Not Parse", "Error")
}
else 
{
  MessageBox.Show("My int value is " + result.ToString());
}


Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top