从.net Data.ToBinary() 中提取数据
我需要读取使用 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;
附:这个概念是否有效……或者日期是以总毫秒的形式存储的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用任何您想要的格式,只要同时进行序列化和序列化即可。反序列化:)但是既然常见的表示是“Ticks”,为什么不这样做呢?
序列化就是关于保存和保存。恢复,只要您可以恢复您保存的任何内容,序列化就有效:)
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 :)
使用 Int64 构造函数,但应注意 DateTimeKind 参数。如果序列化 DateTimeKind.Utc 并反序列化 DateTimeKind.Unspecified 或 DateTimeKind.Local 值,可能会遇到麻烦。
注意: 最安全的做法可能是使用 ToUniversalTime() 序列化 DateTime 值,并始终使用 DateTimeKind.Utc 构造返回值。
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.