- Home /
Can't Convert string binary to long.
Hello everyone,
I have a problem, it might be a trivial problem though..
I need to save the last date and time when the user leaves the game or quits the game. And get back when starts the game again, just to calculate the difference in time, since there are factors that will be decremented with respect to time.
void Start ()
{
CalculateTimeDifference();
}
void OnApplicationPause(bool pauseStatus)
{
paused = pauseStatus; // Check the state of the application
if(paused)
PlayerPrefs.SetString("sDate",System.DateTime.Now.ToBinary().ToString());
if(!paused)
CalculateTimeDifference();
}
void OnApplicationQuit()
{
PlayerPrefs.SetString("sDate",System.DateTime.Now.ToBinary().ToString());
}
private void CalculateTimeDifference()
{
currentDate = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
DateTime oldDate = DateTime.FromBinary(temp);
TimeSpan difference = currentDate.Subtract(oldDate);
}
When CalculateTimeDifference() is called in the start function, it gives me ar error. The errors says:
"FormatException: Input string was not in the correct format System.Int64.Parse (System.String s)"
I can't guess what might be wrong with this ?? any help please
Are you sure PlayerPrefs.GetString("sysString")
returns a string? Is it set?
Try to decompose your code:
string __temp = PlayerPrefs.GetString("sysString");
print(__temp);
long temp = Convert.ToInt64(__temp);
Then that will tell you how the string looks. I think those Convert methods do not like if you have anything else but numbers, so if for some reasons an extra character is there, that would clash. For instance a float.
FormatException : value does not consist of an optional sign followed by a sequence of digits (0 through 9).
It appeared my mistake!
It seems that OnApplicationPause is being called not only after pausing and returning the game, but every time the game starts (I am testing in the editor), I moved my function call in Start and it got solved.
All right, please post an answer and accept it, so the question gets closed. :)
Answer by moghes · Oct 15, 2013 at 11:43 AM
OnApplicationPause was called everytime I start the game, and it is called before the Start function.
I added
if(!paused)
{
if(PlayerPrefs.HasKey("sysString"))
{
CalculateTimeDifference();
}
}
CalculateTimeDifference() function Gets from PlayerPrefs, while the first time or after (PlayerPrefs.DeleteAll), so there was no value (null) for PlayerPrefs.GetString("sysString"), as mattssonon helped. Adding PlayerPrefs.HasKey("sysString") will not call the function if no key, plus I removed the function call from the Start function.
Answer by ArkaneX · Oct 14, 2013 at 09:26 AM
You're writing sDate
but reading sysString
.
thanks dude for your reply, but that's been proper in my code.. I've been pasting portions of my code just to avoid huge posts and thats by mistake
Your answer
Follow this Question
Related Questions
String format to show float as time 3 Answers
Export objects to a .3DS file at runtime 1 Answer
How to stop WWW from changing request url? 1 Answer
what package?namespace? do I need to make use of String.Format() for C# programs? 4 Answers
FormatException: Input string was not in the correct format 1 Answer