14,734,709 members
See more:
Iam trying to convert string to integer. i couldnt convert. It throws error as
`Input string was not in a correct format.`

```string snewidstring = "462414720-60TX6E0088";
int snewid = Convert.ToInt32(snewidstring);```

How to convert this string to integer.

What I have tried:

```<pre>string snewidstring = "462414720-60TX6E0088";
int snewid = Convert.ToInt32(snewidstring);```
Posted
Updated 27-Jul-18 4:55am
Bryian Tan 25-Jul-18 23:57pm

Just curious, what is the expected result?
Dave Kreskowiak 26-Jul-18 0:16am

What did you expect was going to happen? That string has a hyphen and alphabetic letters in it. There's no way that's going to convert to an integer value.

You're going to have to define, in precise terms, what you expect to get out of that string and what you expect to be able to convert to an integer.
#realJSOP 27-Jul-18 11:12am

He was on an adventure of discovery.
Patrice T 26-Jul-18 1:53am

What integer did you expect for this string ?
#realJSOP 27-Jul-18 11:12am

"Oranges".
Patrice T 27-Jul-18 11:52am

you need to use more magic
Gerry Schmitz 26-Jul-18 4:22am

Is this what your boss told you to do? "Convert it to an int" ...
#realJSOP 27-Jul-18 11:13am

He's getting his instructions from a golden unicorn, covered in glitter, and shooting rainbows out of its ass.

Solution 1

#realJSOP 27-Jul-18 11:10am

This is the answer. It's obvious that the string he's trying to convert is NOT a valid number. One of the integer `TryParse()` methods is the right way to go. Whoever voted you a "1" is a retard, and should reconsider that maybe their best possible vocation is sweeping standing water off sidewalks, or watching the machine that puts the ridges on the edges of checkers.

Solution 2

```string snewidstring = "462414720-60TX6E0088";
int storein;
int.TryParse(snewidstring, out storein);
```
F-ES Sitecore 26-Jul-18 5:54am

You can improve the efficiency of that code without change what it does by making this slight change;

string snewidstring = "462414720-60TX6E0088";
int storein = 0;

Solution 3

Well, it seems you at least tried... You're going to have to remove the - and TX and E which will give you a large number, so you'll have to go Int64.

This is how your string will convert.
```string snewidstring = "4624147206060088";
Int64 snewid = Convert.ToInt64(snewidstring);```
#realJSOP 27-Jul-18 11:25am

That code will throw exactly the same exception.
Dylvh 31-Jul-18 10:31am

Have you tried the code to see if it throws the exact same error? ;-) It actually does convert without throwing an exception. Anyway, I agree that the tryparse is the correct answer.
#realJSOP 31-Jul-18 11:55am

It will throw the same exception with the OP's example value. You're converting an actual integer string to an int64, so it will convert without an exception. Your answer is therefore incorrect, and my observation stands.
Dylvh 1-Aug-18 3:29am

I understand. Much appreciated.

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

Top Experts
Last 24hrsThis month
 CPallini 145 OriginalGriff 125 Patrice T 125 Richard MacCutchan 105 Richard Deeming 95
 OriginalGriff 3,110 Richard MacCutchan 1,455 CPallini 1,193 Richard Deeming 730 Patrice T 730

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