Reputation: 31
I'm trying to make a small application. It gets an ID in a server response, but it's in string format. I need to convert it to int
, but it doesn't seem to work. That ID is for example 76561198078450726
. Int.TryParse
returns 0
.
Other methods doesn't work either. Do you have any Idea, whats the problem?
Edit: Thanks a lot for your helpful answers. I don't know, how I could miss that. Thanks!
Upvotes: 0
Views: 1295
Reputation: 47600
Int32.MaxValue
is 2,147,483,647. When TryParse fails it returns 0.
Try long.TryParse()
Upvotes: 2
Reputation: 1371
That's a really big number. Int only goes to 2147483647
.
Use long
or take a look at System.Numerics.BigInteger
to go even bigger: https://msdn.microsoft.com/en-us/library/system.numerics.biginteger.aspx
Edit:
Also, int.TryParse
returns true
or false
and that is what you should be checking, not if the out
value is 0. Because int i; int.TryParse("0", out i);
is also completely valid.
Upvotes: 5
Reputation: 98868
Since int.MaxValue
is 2147483647
, this value is too large for an 32-bit signed integer. I would suggest to parse it to long
instead.
long l = long.Parse("76561198078450726");
Upvotes: 2
Reputation: 37113
76561198078450726 is far outside the range of int
(which is -2,147,483,648 to 2,147,483,647), use long
instead:
if (long.TryParse(value, out myValue)) ...
Upvotes: 3