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
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.