3

我需要读取使用 BinaryWriter 类编写的自定义序列化二进制数据。为了存储日期,最初的设计者使用BinaryWriter.Write( Data.ToBinary() );

这篇文章有点提到 ToBinary 函数是如何工作的;但我需要的是构建将模拟其他编程语言中的方法 ToBinary() 和 FromBinary() 的代码。

任何人都可以查看以下伪代码并让我了解实际偏移位计数。

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;

附言。这个概念甚至会起作用..还是以总毫秒的形式存储日期?

4

2 回答 2

1

你可以使用任何你想要的格式,只要你同时进行序列化和反序列化:) 但是既然一个常见的表示是'Ticks',为什么不这样做呢?序列化就是保存和恢复,只要你可以恢复你保存的任何东西,序列化就可以了:)

于 2011-04-07T18:25:08.950 回答
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.

于 2014-03-01T20:41:59.177 回答