3

I need to interact with a service which is returning me a string.... however this is a representation of a Byte Array in text.

Then I need convert this string to a byte array and after convert it to DateTime....

To make things worse, I really do not know what type of encoding is been used.....the only few things I know are 2 samples that were provided to me:

  • Exact received string: "32,6,23,8,69,80,48,36" needs to give me the datetime: 17.6.2020 08:45:50
  • Exact received string: "32,6,23,8,69,87,118,148" needs to give me the datetime: 17.6.2020 17:45:57

Any idea on how to do this, or at least some points that could help me move on into a solution? Thanks.

#Update1:

I have tried the following code but the results are not what I am looking for:

byte[] bytes = { 32, 6, 23, 8, 69, 80, 48, 36 };
long longVar = BitConverter.ToInt64(bytes, 0);
DateTime dateTimeVar = new DateTime(longVar);  // Result: 5/21/8264 9:36:06 AM

string t1 = Encoding.UTF8.GetString(bytes); //  " \u0006\u0017\bEP0$"
string t2 = Encoding.ASCII.GetString(bytes); // " \u0006\u0017\bEP0$"
string t3 = Encoding.BigEndianUnicode.GetString(bytes); // " ᜈ䕐〤"
string t4 = Encoding.UTF32.GetString(bytes); // "��"
string t5 = Encoding.UTF7.GetString(bytes); // " \u0006\u0017\bEP0$"
string t6 = Convert.ToBase64String(bytes);  // IAYXCEVQMCQ=

long longVar = BitConverter.ToInt64({ 32, 6, 23, 8, 69, 87, 118, 148 }, 0); 
// This one even gives me a negative number: -7748910154844273120

#Update2:

I know that the dates are obtained from an OPC server and other systems in between.... what is happening till the data arrives in my service I really have no idea.... Meanwhile I google for OPC and datetime and got the following link about datetimes, maybe I get some clarification.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
Dryadwoods
  • 2,875
  • 5
  • 42
  • 72
  • is this EXACTLY what is being given to you `32,6,23,8,69,80,48,36` – TheGeneral Jun 22 '20 at 08:52
  • @TheGeneral I know how to do byte[] bytes = { 32, 6, 23, 8, 69, 80, 48, 36 }; this is not the problem... What I want to know is how to bring it back to DateTime – Dryadwoods Jun 22 '20 at 08:54
  • Can you share the exact string you are getting from the service ? – Chetan Jun 22 '20 at 08:55
  • 1
    Are you asking us to guess the format? – ProgrammingLlama Jun 22 '20 at 09:07
  • 3
    @Dryadwoods - What do the numbers `32, 6, 23, 8, 69, 80, 48, 36` represent? How do they relate to a `DateTime`? – Enigmativity Jun 22 '20 at 09:08
  • 1
    Here is the rub, those bytes dont relate to ticks / a long version of a DateTime, you are missing vital informaiton – TheGeneral Jun 22 '20 at 09:08
  • 1
    @Dai - `.Reverse()` doesn't seem to help. – Enigmativity Jun 22 '20 at 09:10
  • 1
    I doubt its unix time, its not in hex, its not ticks, changing endian doesn't help, i give up – TheGeneral Jun 22 '20 at 09:13
  • 1
    When the bytes are interpreted as big-endian unsigned 64-bit integers, the difference between them is `476784` - and this is meant to represent a time-difference of about `9h, 0m, 7s`. – Dai Jun 22 '20 at 09:16
  • 1
    *"Then I need convert this string to a byte array..."* You have given us a byte array, so you have already done this step. Maybe something went wrong there? Can you give us the original string (for example, as a list of unicode code points)? – Heinzi Jun 22 '20 at 09:48
  • Working answer: https://stackoverflow.com/a/62576969/766304 – Risord Apr 27 '21 at 10:13

0 Answers0