Veskechky
Veskechky

Reputation: 173

Binary to Date (C#) 64 Bit Format

We have a binary file from which we have identified the following dates (as Int64). We now the following facts about the Date/Time format;

Any help on figuring out a way to convert this to a valid C# Date/Time is appreciated.

Upvotes: 2

Views: 6263

Answers (4)

lithium81
lithium81

Reputation: 56

Here is a solution:

string hex_data = "7DA34FFFFFFFFFC0";
        Int64 int_datetime = Convert.ToInt64(hex_data, 16);

        int year = (int)(int_datetime >> 52);
        int month = (int)((int_datetime >> 48) & 0x0F);
        int day = (int)(((int_datetime >> 43) & 0x1F));

        int hour = (int)(((int_datetime >> 38) & 0x1F)) > 24 ? 0 : (int)(((int_datetime >> 38) & 0x1F));
        int min = (int)(((int_datetime >> 32) & 0x3F)) > 60 ? 0 : (int)(((int_datetime >> 32) & 0x3F));
        int sec = (int)(((int_datetime >> 26) & 0x3F)) > 60 ? 0 : (int)(((int_datetime >> 26) & 0x3F));
        int mili_sec = (int)(((int_datetime >> 16) & 0x3FF)) > 100 ? 0 : (int)(((int_datetime >> 16) & 0x3FF));
        int micro_sec = (int)(((int_datetime >> 6) & 0x3FF)) > 100 ? 0 : (int)(((int_datetime >> 6) & 0x3FF));

        string str_date_time = year.ToString("D4") + "/" + month.ToString("D2") + "/" + day.ToString("D2") + " " + hour.ToString("D2") + ":" + min.ToString("D2") + ":" 
            + sec.ToString("D2") + "." + mili_sec.ToString("D3");

        DateTime date_time_dt=DateTime.Parse(str_date_time);

Returns:

09/03/2010 12:00:00 AM

in the date_time_dt object. I don't think the DateTime object supports microseconds in C#, but I may be wrong.

Upvotes: 0

Chris Haas
Chris Haas

Reputation: 55457

Going off of what @BlueRaja The Green Unic said, here's how you'd parse it:

        var X = 0x7da34fffffffffc0L;
        var Year = X >> 52;
        var Month = (X >> 48) & 0xf;
        var Day = (X >> 43) & 0x1f;

Upvotes: 2

0x7da = 2010
0x3 = 3 (March)
01001111 = the first 5 bits seem to be the day (9), the last three are always 1 from what you've shown us
The rest of the bits probably represent further resolution (hours, minutes etc.)

Upvotes: 2

unwind
unwind

Reputation: 400029

From the hex data, at least this much is clear:

0x7da = 2010

The 3 next is very likely March (month 3).

Upvotes: 2

Related Questions