将datetime c ++ struct(以字节形式接收)转换为c#datetime实例(Convert datetime c++ struct (received as bytes) to c# datetime instance)

所以我正在努力替换用c ++编写的遗留应用程序,我在将网络通信中使用的结构映射到c#时遇到了一个小问题。 基本上tcp连接的另一端使用以下结构来写日期,我不知道如何将通过序列化该结构生成的字节转换为ac#datetime。 大多数情况下很容易到达“millis”和“second”,它们分别由10位和6位组成,因此它们之间共享2个字节。 我假设您通过位移来解决这个问题,以便将值读取和写入字节数组,但我对此没有任何经验。

typedef struct DATE_TIME { USHORT year; UCHAR month; UCHAR day; UCHAR hour; UCHAR minute; USHORT millis : 10; USHORT second : 6; }

当前尝试阅读的代码

ushort Year = br.ReadUInt16(); byte Month = br.ReadByte(); byte Day = br.ReadByte(); byte Hour = br.ReadByte(); byte Minute = br.ReadByte(); ushort secAndMillSec = br.ReadUInt16(); ushort Milliseconds = (ushort)(secAndMillSec >> 6); ushort Seconds = (ushort)((ushort)(secAndMillSec << 12)>>12);

我第一次尝试写代码

bw.Write(Year); bw.Write(Month); bw.Write(Day); bw.Write(Hour); bw.Write(Minute); ushort secAndMillSec = (ushort)(Milliseconds << 6); secAndMillSec = (ushort)(secAndMillSec + Seconds); bw.Write(secAndMillSec);

看起来又好吗? 到目前为止,我可以运行它的所有测试数据是空日期,所以我有自己测试的问题

So i am working on replacing a legacy app that was written in c++ and i have hit a small issue in mapping a struct used in network communications to c#. Basically the other end on the tcp connection uses the following struct to write dates and i have no idea how to convert the bytes generated by serialising that struct to a c# datetime. Most of it is easy till you get to the "millis" and "second" which are made up of 10 bits and 6 bits respectively so that the 2 bytes is shared between them. I assume you solve this with bit-shifting to read and write the values to a byte array but i have no experience with this.

typedef struct DATE_TIME { USHORT year; UCHAR month; UCHAR day; UCHAR hour; UCHAR minute; USHORT millis : 10; USHORT second : 6; }

Code for current attempt to read

ushort Year = br.ReadUInt16(); byte Month = br.ReadByte(); byte Day = br.ReadByte(); byte Hour = br.ReadByte(); byte Minute = br.ReadByte(); ushort secAndMillSec = br.ReadUInt16(); ushort Milliseconds = (ushort)(secAndMillSec >> 6); ushort Seconds = (ushort)((ushort)(secAndMillSec << 12)>>12);

Code for my first try at a write

bw.Write(Year); bw.Write(Month); bw.Write(Day); bw.Write(Hour); bw.Write(Minute); ushort secAndMillSec = (ushort)(Milliseconds << 6); secAndMillSec = (ushort)(secAndMillSec + Seconds); bw.Write(secAndMillSec);

Again does it look right? So far all the test data i can run it against is empty dates so i am having issues testing myself

最满意答案

位字段在编译器中不可移植,但我们可以假设这两个字段位于两个后续字节中。

我假设您正在从网络接收流,并且您正在阅读日期时间。

BinaryReader reader = new BinaryReader(networkStream); int year = reader.ReadUInt16(); int month = reader.ReadByte(); int day = reader.ReadByte(); int hour = reader.ReadByte(); int minute = reader.ReadByte(); int ms = reader.ReadUInt16(); int second = ms >> 10; int millist = ms & 1023; DateTime dt = new DateTime(year, month, day, hour, minute, second, millis);

Bit fields are not portable across compilers but we can assume that these two fields are in two consequent bytes.

I assume you are receiving a stream from the network, and you are at the point of reading the datetime.

BinaryReader reader = new BinaryReader(networkStream); int year = reader.ReadUInt16(); int month = reader.ReadByte(); int day = reader.ReadByte(); int hour = reader.ReadByte(); int minute = reader.ReadByte(); int ms = reader.ReadUInt16(); int second = ms >> 10; int millist = ms & 1023; DateTime dt = new DateTime(year, month, day, hour, minute, second, millis);

更多推荐