Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Int.Parse throws invalid format error, but the string seems to be a valid integer

Discussion in 'Scripting' started by McHound, Feb 9, 2023.

  1. McHound

    McHound

    Joined:
    Oct 2, 2019
    Posts:
    3
    Hi, I have a <string,string> dict, and some of the values contain an integer. Later, I'm trying to parse an int value like
    Code (CSharp):
    1. Int.Parse(myDict[key])
    .
    It throws a format error even though I'm sure it contains just a single number as the string, after checking with debugging and Debug.Log.
    I'm collecting the numbers from a TMP_textfield. Could this part of the issue somehow?

    Thanks!
     
  2. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Can you show an example of what should be a valid string (taken straight from the logs)?
     
  3. McHound

    McHound

    Joined:
    Oct 2, 2019
    Posts:
    3
    Code (CSharp):
    1. Debug.Log(spaceShipStats["ShipAmount"]);
    prints

    Code (CSharp):
    1. 1
    2. UnityEngine.Debug:Log (object)
    3. BattleController:ConstructShips () (at Assets/Scripts/BattleController.cs:86)
    4. UnityEngine.EventSystems.EventSystem:Update () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Well, if some of your values aren't integers, you should probably be using TryParse anyways. Try outputting it from here what you get before and after using it.
     
    Bunny83 likes this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Well, int.Parse does not have any room for interpretation. When it says there's something wrong with the string, there is something wrong with it. You said you "printed it out", which indeed is the first step, but does not rule out any issues with the string. Leading or trailing spaces, control characters, null character of byte order marks would cause such an error. The next simple step is to also print out the Length of the string. Since you said (and the log seems to confirm) that the string only contains a single character, the Length should be 1. Any larger value already indicates that there are other characters involved.

    Try adding
    using System.Linq;
    at the top and print this instead:

    Code (CSharp):
    1. string hexStr = str.Aggregate("", (a, c) => a + ((int)c).ToString("x4")+" ");
    It will convert each character in the string into the corresponding hex code. That way you will see every character that there is and you can also identify what those characters actually are. If you can't interpret the output yourself, post it here.
     
  6. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    I believe that Int.Parse tolerates whitespace -- but it's definitely possible that there're some weird invisible characters in there. Text is always such a joy to deal with...
     
    Bunny83 likes this.
  7. McHound

    McHound

    Joined:
    Oct 2, 2019
    Posts:
    3
    Hi all.

    The problem indeed seemed to be extra invisible characters in the string. When looking at the textInfo, the single digit contained 9 or so characters... o_O

    I obtained the string from the TMP_Text object inside a TMP_InputField - but directly using the InputField.text magically fixed the problem.

    Thanks for the suggestions!
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    Yes, you're right, it seems that spaces, tabs and new line characters are allowed as leading and trailing characters. It even allows the null character as a trailing character. Though as a leading character it would still fail.

    Well I guess it may have added some formatting rich text tags which are not displayed in the console, as the console can also interpret those :) That's why just printing out strings does not necessarily tell the whole story. I recently had a similar issue at my job (not in Unity). I was printing stuff into the browser console but it also interpreted / stripped certain character I did not expect to get stripped ^^.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,945
    There's a lot to this. The text field you got that from can be elided (Such as when you type a very long string that scrolls off the left), plus I think it has these weird UTF-8 inviso spaces jammed in for things, plus possibly an inverted thin space character for the caret.

    Always get your data from the source, which in this case is InputField.text. Same applies for the TMPRo versions of InputField and TextMesh

    I remember discovering this first-hand in 2014 and it baffled our entire team for a day until we nailed it down.
     
    chemicalcrux likes this.
  10. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    Oh! I completely missed that.

    That'll do it.
     
    Bunny83 likes this.