0
using (MemoryStream mem = new MemoryStream (bytes.Skip(4).toArray()))
{
  using (BinaryReader reader = new BinaryReader(mem))
  {
  UInt32 time1;
  UInt32 time2;
  Int64 time = 0;

    try{
    time1 = reader.ReadInt32();
    time2 = reader.ReadInt32();
    time = (((Int64)time2) << 32) | time1;

    return new DateTime(time);
    }

    catch{
    }
  }
}


I know this code is dealing with signed/unsigned integers and bit shifting, but I am not exactly sure I understand it as a whole. Could someone explain? Also what would be the reason behind this code giving an OutOfRange exception saying the number must be non-negative and less than or equal to Int32.maxvalue or -1 when this code is called in a Thread.Sleep?

  • What language is this? As far as I can tell, it concatenates time2 and time1 together to give you a 64bit integer, but that 64bit integer should be unsigned as well. And I don't know this particular language, but that 64bit integer doesn't look unsigned to me. – Stephan Branczyk Oct 05 '22 at 15:20
  • [BinaryReader.ReadInt32](https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readint32?view=net-6.0#system-io-binaryreader-readint32) returns a **signed** 32bit integer value. you try to shove a number `-2147483648 <= n <= 2147483647` into a variable that can take `0 <= n <= 4294967295` – Thomas Oct 05 '22 at 15:31
  • @Thomas would making Int64 into an unsigned int fix this problem ? – duk3th3c0d3r Oct 05 '22 at 15:49
  • @StephanBranczyk this is C# used in visual studios. – duk3th3c0d3r Oct 05 '22 at 15:51
  • "@Thomas would making Int64 into an unsigned int fix this problem?" No, based on what Thomas is saying, that means you would need to declare time1 and time2 as Int32, not UInt32. – Stephan Branczyk Oct 05 '22 at 20:28

0 Answers0