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

webrequest.text cannot convert to int

Discussion in 'Scripting' started by Phoenix7, Jan 15, 2020.

  1. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    I'm completly confused. Somthing went wrong...everytime when I used a webrequest from a website, I can convert the text to int..sometimes for a highscore board (or something else)..
    Now, its working completly not. :-(

    I can connect to the server, I can read the content (simply a number)..but I CONNOT convert or parse this into an INT

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5.  
    6. public class MyBehaviour : MonoBehaviour
    7. {
    8.     public int wert;
    9.     public int wertA;
    10.     public string wertstring;
    11.     public int BLahINT;
    12.     public int BLahINT2;
    13.     void Start()
    14.     {
    15.         StartCoroutine(GetText());
    16.     }
    17.     IEnumerator GetText()
    18.     {
    19.         UnityWebRequest www = UnityWebRequest.Get("http://sshag.ch/goldenTipp.php/");
    20.         yield return www.SendWebRequest();
    21.  
    22.         if (www.isNetworkError || www.isHttpError)
    23.         {
    24.             Debug.Log(www.error);
    25.         }
    26.         else
    27.         {
    28.             // Show results as text
    29.  
    30. //Try 1 -> OK See the Number 24
    31.             wertstring = www.downloadHandler.text.ToString();
    32.             Debug.Log(wertstring);
    33.  
    34. //Try 2 -> FAIL Parse the STRING = 0
    35.             wertA = int.Parse(wertstring);
    36.             Debug.Log(wertA);
    37.  
    38. //Try 3 -> FAIL Parse the www text directly = 0
    39.             wert = int.Parse(www.downloadHandler.text);
    40.             Debug.Log(wert);
    41.  
    42. //Try 4 -> FAIL parse the www text into int = 0
    43.             int.TryParse(www.downloadHandler.text, out BLahINT);
    44.             Debug.Log(BLahINT);
    45.  
    46. //Try 5 ->FAIL String to Int = 0
    47.             int.TryParse(wertstring, out BLahINT2);
    48.             Debug.Log(BLahINT2);
    49.  
    50.             // Or retrieve results as binary data
    51.             byte[] results = www.downloadHandler.data;
    52.         }
    53.     }
    54. }
    55.  
    Of course its not matter if the number on the website a result from a database request or is it only a numer:

    my PHP Code:

    <?php
    $pdo = new PDO('mysql:host="";dbname=""', 'xyz', 'xyz');
    $statement = $pdo->prepare("SELECT TippID, COUNT(*) AS anzahl FROM hilfe GROUP BY TippID ORDER BY anzahl DESC LIMIT 1");
    $statement->execute();
    while($row = $statement->fetch()) {
    echo $row['TippID'];
    }
    ?>


    I tested with Unity 2018.4.15f1 / 2019.3.0f5 / 2019.2.17f1
    Complettly reinstall -> the same
    Reinstall of MS Visual Studio, .Net, Android SDK/NDK/Resolver

    My error:

    Code (CSharp):
    1. FormatException: Input string was not in a correct format.
    2. System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    3. System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    4. System.Int32.Parse (System.String s) (at <e1319b7195c343e79b385cd3aa43f5dc>:0)
    5. MyBehaviour+<GetText>d__6.MoveNext () (at Assets/_script/MyBehaviour.cs:34)
    6. UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
    7.  
    Error Line MyBehaviour.cs:34:
    Code (CSharp):
    1. wertA = int.Parse(wertstring);
    I test the variable with GetType: (From Try 1)

    Debug.Log(wertstring.GetType());
    Result:
    System.String
    UnityEngine.Debug:Log(Object)
    <GetText>d__6:MoveNext() (at Assets/_script/MyBehaviour.cs:36)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    It Is a STRING!

    So I need help..do you have any ideas?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    My first thought is that there may be something strange about the string's format that's not visible to the human eye when it's as text - some weird Unicode encoding issue or something.
    If you output wertstring.Length, is it 2?
    If you do this to check the exact ASCII codes of the string:
    Code (csharp):
    1.  
    2.         for (int i=0;i<wertstring.Length;i++)
    3.         {
    4.             Debug.Log(wertstring[i] + " is " + (int)wertstring[i]);
    5.         }
    Do you get "2 is 50" and "4 is 52"?
     
  3. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    mmm..with your code I see 3 Debug.Log entrys:
    1.
    is 65279
    UnityEngine.Debug:Log(Object)
    <GetText>d__7:MoveNext() (at Assets/script/MyBehaviour.cs:35)

    2.
    2 is 50
    UnityEngine.Debug:Log(Object)
    <GetText>d__7:MoveNext() (at Assets/script/MyBehaviour.cs:35)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    3.
    4 is 52
    UnityEngine.Debug:Log(Object)
    <GetText>d__7:MoveNext() (at Assets/script/MyBehaviour.cs:35)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

    with the length:
    Code (CSharp):
    1. Debug.Log("länge:" + wertstring.Length);
    it write: 3
    because why?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Alright, so yeah there is an extraneous character in there, a zero-width non-breaking space, which is definitely one of those "invisible Unicode characters that can break stuff" characters. I'm not sure why it's there, but I'm not much for server side code so I dunno what exactly that bit of PHP is doing. (The fact that it's non-breaking is actually important. Unicode treats non-breaking characters much the same way as letters as opposed to whitespace, so what the parse functions see here is much more similar to if the string contained "f24" as opposed to, say, " 24" (with a space); the parse functions can and do trim off whitespace, but not non-breaking characters.)

    First, try
    Code (csharp):
    1. wertstring = wertstring.Substring(1);
    before you parse wertstring, and see if you get the expected results. That's probably not a great longterm solution (if we don't know why that weird character is there we can't rely that it will always be there, and we don't want to trim "345" to "45" if the server stops inserting that character), but it should confirm that this was the issue.

    You might try something like this:
    Code (csharp):
    1. wertstring = wertstring.Trim((char)(65279));
    That should trim that specific zero-width character off the edges of the string before you process it, and won't mangle the text if you later start getting a response without that character. (untested)
     
  5. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Iwill testing.. :) Thanks for your answer. I will write back about the result !
     
  6. Phoenix7

    Phoenix7

    Joined:
    Mar 18, 2014
    Posts:
    22
    Hello again..I can SAY: IT WORKS. :)

    Code (CSharp):
    1. wertstring = goldtipp.text.ToString();
    2.             wertstring =  wertstring.Substring(1);
    3.             wertstring = wertstring.Trim((char)(65279));
    4.             Debug.Log(wertstring);
    5.             Tipp_Nr(int.Parse(wertstring));
    The var werstring is now a INT and I can use it for the function and the result works.

    Thanks a lot.

    Something strange:
    An the same webserver, with the same DB I read some other variable with the same code I become te number without the invisible Code..?!
     
  7. Haraigoshi

    Haraigoshi

    Joined:
    Mar 24, 2019
    Posts:
    1
    Thanks a lot for this! Was running into the same problem with unity webrequest getting data from a textfile from the server. This solution worked for me.
     
  8. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    An alternative approach could be to use a regular expression to just extract the numerical characters, then feeding that to Parse. That would work regardless of whatever other garbage ends up in the strings you're parsing.