3

I need to read custom-serialized binary data, written using BinaryWriter class. To store a date, the original designers used BinaryWriter.Write( Data.ToBinary() );

This article sort-of mentions how the ToBinary function works; but what I need, is to build code that will emulate the methods ToBinary() and FromBinary() in other programming languages.

Can anyone look at the following pseudo code and give me an idea of the real offset bit counts.

long i = DateTime.Now.ToBinary();
// will likely need to add code here to "condition" the value
int yr = (i >> 48) & 0x7fff;
int mo = (i >> 44) & 0xf;
int day = (i >> 36) & 0xff;
int hr = (i >> 28) & 0xff;
int min = (i >> 20) & 0xff;
int sec = (i >> 12) & 0xff;
int ms = i & 0xfff;

ps. will this concept even work..or is the date stored in the form of total milliseconds?

Community
  • 1
  • 1
user697296
  • 51
  • 6
  • Are you trying to read the serialized data from .net in another language without the framework? – sarvesh Apr 07 '11 at 18:23
  • That is correct. languages including java and objective-C. language conversion is trivial once we know the exact schema. – user697296 Apr 08 '11 at 12:56

2 Answers2

1

You can use any format you want, as long as you do both the serialize & deserialize :) But since a common representation is 'Ticks', why not go with that? Serialization is all about saving & restoring, as long as you can restore anything you save, serialization worked :)

NKCSS
  • 2,716
  • 1
  • 22
  • 38
  • thanks, but the Ticks property of a DateTime is read-only. It's too late to choose the format we like; There is a lot of data in the legacy format we need to access anywhere. Additional foresight would have had us build our own date-time serialization format. – user697296 Apr 08 '11 at 12:53
  • 2
    The DateTime constructor can take Ticks as a parameter to instantiate the value... http://msdn.microsoft.com/en-us/library/aa326681(v=vs.71).aspx – NKCSS Apr 08 '11 at 13:00
0

Use the Int64 constructor, but you should be careful about the DateTimeKind argument. You can get into trouble if you serialize DateTimeKind.Utc and deserialize a DateTimeKind.Unspecified or DateTimeKind.Local value.

Note: Probably the safest thing to do is serialize your DateTime values using ToUniversalTime() and always construct the return value using DateTimeKind.Utc.

Ben Stabile
  • 173
  • 1
  • 4