Convert String to Int in C#

Hi,
I am new to Unity. I am working in Android program. I need to convert String to Int.

Is there any way to convert String to Int.

Eg:
String tempString = “4”;

This tempString i need to convert to Int.

Thanks
Varadharaj.

5 Likes

int.Parse, or TryParse.

–Eric

21 Likes

int.Parse(tempString);

There is also int.TryParse which I usually use, you can lookup it’s usage on google.

7 Likes

Hi,
I am working on android application. I need difference between “Screen.width” and “Screen.currentResolution.width”.

Thanks,
Varadharaj.

3 Likes
// declare a string variable with a value that represents a valid integer
string sillyMeme = "9001";

int memeValue;
// attempt to parse the value using the TryParse functionality of the integer type
int.TryParse(sillyMeme, out memeValue);

Debug.Log(memeValue);//9001
15 Likes

I know this post has been answered already, but for those who are looking for benchmarks, he benchmarks several different ways to convert a string to an int:

http://blogs.davelozinski.com/curiousconsultant/csharp-net-fastest-way-to-convert-a-string-to-an-int

What’s interesting is the fastest way isn’t using any of the native C# methods Convert.ToInt32(), int.TryParse(), or int.Parse().

Definitely worth a read for those that are curious.

5 Likes

The C# language has a Convert class that allows you to convert a String to an Integer.

int value = Convert.ToInt32(no);

More info…How to convert string to integer in C#

richard

1 Like

int myConvertedInt = int.Parse(,System.Globalization.NumberStyles.Integer)

1 Like

Just adding another piece to the mix… I found a great method in this thread, in the answer by drudiverse. Here is the code for a very fast conversion from string to int. I added my own overload for char to int as well:

// Benchmark:
// int.Parse("400")     123.07 ns
// IntParseFast("400")    2.87 ns

     public static int IntParseFast(string value)
     {
     int result = 0;
     for (int i = 0; i < value.Length; i++)
     {
         char letter = value[i];
         result = 10 * result + (letter - 48);
     }
     return result;
     }

   public static int IntParseFast(char value)
     {
         int result = 0;
         result = 10 * result + (value - 48);
         return result;
     }
1 Like

that second method is pretty useless, just evaluates to ‘value - 48’ every time.

It is a shortcut to converting char to int. You could do that in the code, but if you use it more than once you’d better have it in a method, which is my case.

No - his point is that result is initialized to 0. 10 * 0 is always 0 so result is always value - 48

Um… That’s the point? Value is an argument, so it will always purposely evaluate to value - 48.

The Char ‘1’ = 49
The Number 1 = 1
49 - 48 = 1

The Char ‘2’ = 50
The Number 2 = 2
50 - 48 = 2

And so on.

Passing the value ‘1’ as a char will give you 1 back as an int fast.

However, won’t it work without multiplication?

  • public static int IntParseFast(char value)
  • {
  • int result = 0;
  • result = result + (value - 48);
  • return result;
  • }

Just have a single line:
return value - 48

1 Like

Indeed. If we’re talking about micro-optimizations (and we are) then there’s no reason to do the other stuff.

using version 5.4.1f1 of unity.

here is a piece of my code:

using UnityEngine;
using UnityEngine.EventSystems;
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
...
  private int enteredValueCommodityAmount;
...
  private int setCommodityAmountInt()
{
enteredValueCommodityAmount=int.Parse("123456789");

Debug.Log(enteredValueCommodityAmount.GetType());
Debug.Log(enteredValueCommodityAmount);

return enteredValueCommodityAmount;
}

this is the error i am receiving:
FormatException: Input string was not in the correct format

I have tried:
Int32.Parse, system.Int32 and many more that I have found in examples of and in the MSDN documentation and all lead back to the same error.

FormatException: Input string was not in the correct format

int.TryParse throws this error:
error CS1501: No overload for method TryParse' takes 1’ arguments

I was calling my string from a variable, but recently i just tried using a string, like the one above. All to the same effect…

any ideas?
What am i missing?

The necroing in this thread is off the walls.

Also please use code tags.

1 Like

I’m responding to this thread so I can find it again in 2020 and bring it back.

5 Likes

thanks for the link, i checked here:
Wikipedia
couldn’t remember the tags for newline or code. So it is nice that you provided an answer for the format issue.

But why the trolling on the question, did I not present the question well?
I didn’t see the need to create a new thread for the same problem, maybe others might find this problem in a similar situation.

i am missing the “out parameter” in the second error, and this seems to be where the problem lies for TryParse, but for the others above Int32.parse etc i am confused why my string is not valid, i was hoping to get some positive feedback

But really, Bravo on stating the obvious and only offering advice on the formatting. Thanks so much.

The threads original question was about how to convert strings to integers. You actually have a new problem as your integer string is not being parsed which warrants a new thread.

However, with that being said… I don’t see any reason for your string to fail the parse. There is a possibility that your NumberStyles are messed up. Is the TryParse method working for you?