Good day to all, please help me understand, there is a binary file from which I read the file values, and I need to convert from doube to DateTime, the binary itself is being formed in C ++ Builder, later it will be rewritten ...

using (BinaryReader reader = new BinaryReader(File.Open(path, FileMode.Open))) { // пока не достигнут конец файла // считываем каждое значение из файла while (reader.PeekChar() > -1) { double t_n = reader.ReadDouble(); double t_k = reader.ReadDouble(); var time = Convert.ToDateTime(t_n); } } 

I get the error:

An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll

Additional information: Invalid casting "Double" to "DateTime". Tried it like this:

 var tn_n = DateTime.FromOADate(t_n); 

I get the error:

An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Not a valid OleAut date.

help me please

  • five
    The question is how this date was converted to double during recording. - VladD
  • @VladD In the source for the Buildere variables: TDateTime t_n; TDateTime t_k; TDateTime t_n; TDateTime t_k; and further in the code: StringGrid16->Cells[i+1][1]= FormatDateTime("d.mm.yy' / 'h:mm",(per[j].t_n)); Unfortunately, this is the maximum that I can give you now, because I now have only a program on Buildere which is also engaged in reading. Just based on the above, I thought DateTime would do - Ethernets
  • This code formats TDateTime and outputs it to a table, but I wonder how it is written to the file. - VladD
  • @VladD Thanks for the answer, tomorrow I will look, and accomplish my goal. Thanks again for your time - Ethernets
  • one
    @Ethernets: Yep, this is TDateTime . Apparently, DateTime.FromOADate should be correct . And on what value the exception takes off? What is the value of TDateTime ? - VladD

1 answer 1

Investigation in the comments showed that the initial value in C ++ Builder was of type TDateTime .

TDateTime stores time in OLE Automation date format, so it is correct to convert using DateTime.FromOADate .

Please note that not every double value is a valid value for TDateTime , so if you read the wrong double value while reading, the conversion may throw an exception.