-1

When the int variable is more than 10 digits, an error occurs and the number becomes negative.

Why is this happening and how can I solve the problem?

This is my code:

UnityWebRequest www = new UnityWebRequest("https://api.hypixel.net/skyblock/bazaar");
www.downloadHandler = new DownloadHandlerBuffer();
yield return www.SendWebRequest();
JSONNode itemsData = JSON.Parse(www.downloadHandler.text);
unixtimeOnline = itemsData2["lastUpdated"];
Debug.Log(unixtimeOnline);
    // output -2147483648
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Lelouch kun
  • 449
  • 1
  • 13
  • 1
    `int` has a limited range (max 2,147,483,647). Can you use `long` instead? See [docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types) – Hans Kesting Feb 17 '22 at 10:49
  • Thank you, but I want to know why there is a limit for the boxes? – Lelouch kun Feb 17 '22 at 11:27
  • The limit for `int` is 2,147,483,647, so when you try and put a higher value into it Bad Things™️ happen. For instance, when you add 1 to that max value, you get -2,147,483,648. There just isn't enough space in a 32-bit number to store higher values – Hans Kesting Feb 17 '22 at 11:46
  • 1
    @Lelouchkun what "boxes"? – derHugo Feb 17 '22 at 11:47
  • since the time is probably never negative you could also just use `uint`, `ulong` which would give you wider ranges – derHugo Feb 17 '22 at 11:47

2 Answers2

1

tl;dr

Simply use ulong instead of int for unixtimeOnline

ulong unixtimeOnline = itemsData2["lastUpdated"];

What happened?

As was already mentioned int (or also System.Int32) has 32 bits.

The int.MaxValue is

2147483647

no int can be higher than that. What you get is basically a byte overflow.

From the JSON.Parse I suspect you are using SimpleJson

and if you have

int unixtimeOnline = itemsData2["lastUpdated"];

it will implicitly use

public static implicit operator int(JSONNode d)
{
    return (d == null) ? 0 : d.AsInt;
}

which uses AsInt

 public virtual int AsInt
 {
     get { return (int)AsDouble; }
     set { AsDouble = value; }
 }

which is a problem because a double can hold up to

so when you simply do

double d = 2147483648.0;
int example = (int)d;

you will again get

-2147483648

What you want

You want to use a type that supports larger numbers. Like e.g.

  • long: goes up to

    9,223,372,036,854,775,807
    

    and is actually what system time ticks are usually stored as (see e.g. DateTime.Ticks

or actually since your time is probably never negative anyway directly use the unsigned ones

  • ulong: goes up to

    18,446,744,073,709,551,615
    

Solution

Long store short: There are implicit conversion for the other numeric values so all you need to do is use

ulong unixtimeOnline = itemsData2["lastUpdated"];

and it will use AsUlong instead

public static implicit operator ulong(JSONNode d)
{
    return (d == null) ? 0 : d.AsULong;
}

which now correctly uses

 public virtual ulong AsULong
 {
     get
     {
         ulong val = 0;
         if (ulong.TryParse(Value, out val))
             return val;
         return 0;
     }
     set
     {
         Value = value.ToString();
     }
 }
derHugo
  • 83,094
  • 9
  • 75
  • 115
0

As the comment says you will need to use a long variable type