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.

String tempString = “4”;

This tempString i need to convert to Int.

Thanks
Varadharaj.

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

Thanks,
Varadharaj.

// 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
              

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.

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

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;
              

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;
  • 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?

    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.

    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?