fishmong3r
fishmong3r

Reputation: 1434

Converting string or char to int

I'm totally puzzled

string temp = "73";
int tempc0 = Convert.ToInt32(temp[0]);
int tempc1 = Convert.ToInt32(temp[1]);
MessageBox.Show(tempc0 + "*" + tempc1 + "=" + tempc0*tempc1);

I would expect: 7*3=21

But then I receive: 55*51=2805

Upvotes: 8

Views: 11260

Answers (7)

Soner Gönül
Soner Gönül

Reputation: 98740

When you write string temp = "73", your temp[0] and temp[1] are being char values.

From Convert.ToInt32 Method(Char) method

Converts the value of the specified Unicode character to the equivalent 32-bit signed integer.

That means converting a char to an int32 gives you the unicode character code.

You just need to use .ToString() method your your temp[0] and temp[1] values. Like;

string temp = "73";
int tempc0 = Convert.ToInt32(temp[0].ToString());
int tempc1 = Convert.ToInt32(temp[1].ToString());
MessageBox.Show(tempc0 + "*" + tempc1 + "=" + tempc0*tempc1);

Here is a DEMO.

Upvotes: 1

Guffa
Guffa

Reputation: 700152

Converting a character to an integer gives you the Unicode character code. If you convert a string to integer it will be parsed as a number:

string temp = "73";
int tempc0 = Convert.ToInt32(temp.Substring(0, 1));
int tempc1 = Convert.ToInt32(temp.Substring(1, 1));

Upvotes: 1

JLRishe
JLRishe

Reputation: 101652

This works, and is more computationally efficient than using either int.Parse() or Convert.ToInt32():

string temp = "73";
int tempc0 = temp[0] - '0';
int tempc1 = temp[1] - '0';
MessageBox.Show(tempc0 + "*" + tempc1 + "=" + tempc0 * tempc1);

Upvotes: 1

John Willemse
John Willemse

Reputation: 6698

You are getting the ASCII codes for 7 and 3, which are 55 and 51 respectively.

Use int.Parse() to convert a char or string to a value.

int tempc0 = int.Parse(temp[0].ToString());
int tempc1 = int.Parse(temp[1].ToString());

int product = tempc0 * tempc1; // 7 * 3 = 21

int.Parse() doesn't accept a char as a parameter, so you have to convert to string first, or use temp.SubString(0, 1) instead.

Upvotes: 1

L-Four
L-Four

Reputation: 13531

This works:

    string temp = "73";
    int tempc0 = Convert.ToInt32(temp[0].ToString());
    int tempc1 = Convert.ToInt32(temp[1].ToString());
    Console.WriteLine(tempc0 + "*" + tempc1 + "=" + tempc0 * tempc1);           

You have to do ToString() to get the actual string representation.

Upvotes: 1

Habib
Habib

Reputation: 223187

That is the ASCII value for the character 7 and 3. If you want number representation then you can convert each character to string and then use Convert.ToString:

string temp = "73";
int tempc0 = Convert.ToInt32(temp[0].ToString());
int tempc1 = Convert.ToInt32(temp[1].ToString());
MessageBox.Show(tempc0 + "*" + tempc1 + "=" + tempc0*tempc1);

Upvotes: 5

Sayse
Sayse

Reputation: 43300

55 and 51 are their locations in the ascii chart. Link to chart - http://kimsehoon.com/files/attach/images/149/759/007/ascii%281%29.png

try using int.parse

Upvotes: 5

Related Questions