Convert string to int

T

tshad

I am trying to convert a string character to an int where the string is all
numbers.

I tried:

int test;
string stemp = "5";
test = Convert.ToInt32(stemp[0]);

But test is equal to 53.

If I change it to:

test = Convert.ToInt32(stemp);

Then it works the way I want where test = 5.

What I am doing is going through the string one by one and doubling the even
number characters and putting them back into another string:

Here is a snippet of the code where creditCard is a string.
****************************************
string sDigits = null;

for(int ktr=0;ktr<creditCard.Length;ktr++)
{
if (((ktr+1)%2) == 0)
sDigits += Convert.ToString(Convert.ToInt32(creditCard[ktr]) * 2);
else
sDigits += Convert.ToString(creditCard[ktr]);
}
*********************************************

Thanks,

Tom
 
N

Nicholas Paldino [.NET/C# MVP]

tshad,

When you do this:

test = Convert.ToInt32(stemp[0]);

You are taking the first character in the string, '5' and converting
that to a number. The thing is, '5' is a character value with a value of
53, and seen by the CLR as a number, so there is no conversion necessary.
That is why you see 53.

When you do this:

test = Convert.ToInt32(stemp);

It is taking the whole string.

It seems that you want to convert one character at a time to the numeric
value it represents, not the underlying character value. In order to do
that, you need to do this:

test = Convert.ToInt32(stemp[0].ToString());

This will convert the character to a string (giving you "5") and then
convert that to an integer, which will return 5.
 
T

tshad

Hilton said:
string temp = "5";
int test = int.Parse (temp);

That's fine.

But I need to get each character from the string and parse won't work with
stemp[0].

Thanks,

Tom
tshad said:
I am trying to convert a string character to an int where the string is
all numbers.

I tried:

int test;
string stemp = "5";
test = Convert.ToInt32(stemp[0]);

But test is equal to 53.

If I change it to:

test = Convert.ToInt32(stemp);

Then it works the way I want where test = 5.

What I am doing is going through the string one by one and doubling the
even number characters and putting them back into another string:

Here is a snippet of the code where creditCard is a string.
****************************************
string sDigits = null;

for(int ktr=0;ktr<creditCard.Length;ktr++)
{
if (((ktr+1)%2) == 0)
sDigits += Convert.ToString(Convert.ToInt32(creditCard[ktr]) * 2);
else
sDigits += Convert.ToString(creditCard[ktr]);
}
*********************************************

Thanks,

Tom
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Hilton said:
string temp = "5";
int test = int.Parse (temp);

Just note that if the string has not a valid integer Parse will throw an
exception.

You should instead use TryParse , it return a null and use a "out" parameter
to return the int if it could converted it.
 
S

Shishir Kumar Mishra

Some thing like following should help you .

string sampeString = "123456789";
StringBuilder finalString = new StringBuilder();
int count = sampeString.Length;
for(int i = 0; i< count;i++)
{
if((i+1)%2 == 0)
{
finalString.Append(
(Convert.ToInt32(sampeString.Substring(i,1))*2).ToString());
}
else
{
finalString.Append(sampeString.Substring(i,1));
}
}
string output = finalString.ToString();
 
T

tshad

Nicholas Paldino said:
tshad,

When you do this:

test = Convert.ToInt32(stemp[0]);

You are taking the first character in the string, '5' and converting
that to a number. The thing is, '5' is a character value with a value of
53, and seen by the CLR as a number, so there is no conversion necessary.
That is why you see 53.

When you do this:

test = Convert.ToInt32(stemp);

It is taking the whole string.

It seems that you want to convert one character at a time to the
numeric value it represents, not the underlying character value. In order
to do that, you need to do this:

test = Convert.ToInt32(stemp[0].ToString());

That works as does:

test = Convert.ToInt32(stemp.Substring(0,1));

Not sure which is better or more efficient.

Thanks,

Tom
This will convert the character to a string (giving you "5") and then
convert that to an integer, which will return 5.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I am trying to convert a string character to an int where the string is
all numbers.

I tried:

int test;
string stemp = "5";
test = Convert.ToInt32(stemp[0]);

But test is equal to 53.

If I change it to:

test = Convert.ToInt32(stemp);

Then it works the way I want where test = 5.

What I am doing is going through the string one by one and doubling the
even number characters and putting them back into another string:

Here is a snippet of the code where creditCard is a string.
****************************************
string sDigits = null;

for(int ktr=0;ktr<creditCard.Length;ktr++)
{
if (((ktr+1)%2) == 0)
sDigits += Convert.ToString(Convert.ToInt32(creditCard[ktr]) * 2);
else
sDigits += Convert.ToString(creditCard[ktr]);
}
*********************************************

Thanks,

Tom
 
B

Ben Voigt [C++ MVP]

tshad said:
I am trying to convert a string character to an int where the string is all
numbers.

I tried:

int test;
string stemp = "5";
test = Convert.ToInt32(stemp[0]);

But test is equal to 53.

If I change it to:

test = Convert.ToInt32(stemp);

Then it works the way I want where test = 5.

What I am doing is going through the string one by one and doubling the
even number characters and putting them back into another string:

This is at least an order of magnitude faster than anything suggested so
far:

int sum = 0;
foreach( char c in stemp ) {
int digit = c - '0';
// optional -- use if the string isn't already checked for sanity
// if (digit < 0 || digit > 9) throw new ArgumentException("stemp");
if ((digit & 1) == 0) // even
digit <<= 1; // multiply by two
sum += digit;
}
 
N

Nicholas Paldino [.NET/C# MVP]

I don't know what is more efficient (you can easily create tests to do
this), but from a readability standpoint, I would think the indexer, along
with ToString, is easier.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
Nicholas Paldino said:
tshad,

When you do this:

test = Convert.ToInt32(stemp[0]);

You are taking the first character in the string, '5' and converting
that to a number. The thing is, '5' is a character value with a value of
53, and seen by the CLR as a number, so there is no conversion necessary.
That is why you see 53.

When you do this:

test = Convert.ToInt32(stemp);

It is taking the whole string.

It seems that you want to convert one character at a time to the
numeric value it represents, not the underlying character value. In
order to do that, you need to do this:

test = Convert.ToInt32(stemp[0].ToString());

That works as does:

test = Convert.ToInt32(stemp.Substring(0,1));

Not sure which is better or more efficient.

Thanks,

Tom
This will convert the character to a string (giving you "5") and then
convert that to an integer, which will return 5.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I am trying to convert a string character to an int where the string is
all numbers.

I tried:

int test;
string stemp = "5";
test = Convert.ToInt32(stemp[0]);

But test is equal to 53.

If I change it to:

test = Convert.ToInt32(stemp);

Then it works the way I want where test = 5.

What I am doing is going through the string one by one and doubling the
even number characters and putting them back into another string:

Here is a snippet of the code where creditCard is a string.
****************************************
string sDigits = null;

for(int ktr=0;ktr<creditCard.Length;ktr++)
{
if (((ktr+1)%2) == 0)
sDigits += Convert.ToString(Convert.ToInt32(creditCard[ktr]) *
2);
else
sDigits += Convert.ToString(creditCard[ktr]);
}
*********************************************

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top