Parsing to int64 or larger number value

Hey guys, I ran into a problem;
Format Exception: Input string was not in the correct format
FormatException: Input string was not in the correct format
System.Int32.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Int32.cs:629)

I am using a fairly simple save/load function that ‘encrypts’ and decodes a code to load a ship Not exactly sure what the problem is but I am fairly sure that int just doesn’t support a high enough value.

Just not sure how to fix it

I dont have any idea of a more efficient encryption as it has the bare minimum details
‘BlockId(x,y,z(x,y,z|’

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public class GameManager : MonoBehaviour {
    public GameObject Base_Block;
    public GameObject ship;

    [Serializable]
    class shipStringc{
        public string ship;
    }
    //id,position(xyz),rotation(xyz)
    public string shipString = "x(0,0,0(0,0,0|";

    public void saveBuild(){
        BinaryFormatter bf = new BinaryFormatter ();
        FileStream file = File.Create (Application.persistentDataPath + "/shipBuilds.dat");

        shipStringc data = new shipStringc ();
        data.ship = shipString;

        bf.Serialize (file, data);
        file.Close ();
        Debug.Log ("Saved!");
    }

    public void addBlock(int id, Vector3 pos, Quaternion rotation){
        StringBuilder sb = new StringBuilder ();
        sb.Append(id).Append("(").Append (pos.x).Append(",").Append (pos.y).Append(",").Append (pos.z).Append("(").Append (rotation.x).Append(",").Append (rotation.y).Append(",").Append (rotation.z);
        shipString = shipString + sb.ToString () + "|";
    }
    private string shipToLoad;
    public void loadBuild(){
        this.transform.GetComponent<BuildManager> ().clearBuild ();
        if (File.Exists (Application.persistentDataPath + "/shipBuilds.dat")) {
            BinaryFormatter bf = new BinaryFormatter ();
            FileStream file = File.Open (Application.persistentDataPath + "/shipBuilds.dat", FileMode.Open);
            shipStringc data = (shipStringc)bf.Deserialize (file);
            file.Close ();

            shipToLoad = data.ship;
        }

        if (shipToLoad != null) {
            shipToLoad = shipToLoad.Substring (0, shipToLoad.Length - 1);
            string[] blockList = shipToLoad.Split('|');
            foreach (string s in blockList) {
                string[] sector = s.Split ('(');
                string[] vectors = sector[1].Split (',');
                string[] quaters = sector[2].Split (',');
                if (sector [0] != "x") {
                    GameObject block = Instantiate (this.transform.GetComponent<BlockList> ().Blocks [int.Parse (sector [0])].BlockObj,
                        new Vector3 ((int.Parse (vectors [0])), (int.Parse (vectors [1])), (int.Parse (vectors [2]))),
                        new Quaternion ((int.Parse (quaters [0])), (int.Parse (quaters [1])), (int.Parse (quaters [2])), 0));
                    block.transform.parent = ship.transform;
                } else {
                 
                }
                }
            }
        }
}

Did you try to debug log the value of your shipstring? Are you sure you’re not saving floats in a string there?

The save and load works until i get over ~10 blocks then it spits the error. And yes, it only uses numbers when decrypting the rest is strings

Int’s are 32 bits hence the Int32 type shown in the error. Try Int64.Parse() which is a Long in C# or just type long.Parse() which does the same thing.

Says the same thing, just Int64 instead

I don’t think length is an issue anyway, the only time i use ints is at the very last stage which should only be 1-5 characters
Edit-- Yep i did a debug log and they are only 1 digit

Not sure how it’s working up to around 10.
Some other options could be : store the floats in the class & reconstruct your position + rotation.
Either with binaryformatter or JSonUtility.

Ah! found the problem. Turned out it had nothing to do with lengthy values. I have a ‘clear’ button that removed everything on the canvas and it turns out when I press clear i forgot to reset shipString.

Thanks everyone for the help.

:slight_smile: Glad it’s working/fixed.