Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i am reading the current DateTime to byte array how to convert the byte array back to DateTime ?

What I have tried:

C#
public static byte ConvertToBcd(byte x)
{

    int msb = x / 10;

    int lsb = x - (msb * 10);

    msb = msb << 4;

    return (byte)(msb | lsb);

}


public static byte[] GetLocalTimeInBCD()
{

    DateTime now = DateTime.Now;

    byte[] Data = new byte[4];

    Data[0] = (byte)(now.Year - 100);

    Data[1] = (byte)now.Month;

    Data[2] = (byte)now.Day;

    Data[3] = (byte)now.Hour;


    for (int i = 0; i < 4; i++)
    {

        Data[i] = ConvertToBcd(Data[i]);

    }

    return Data;

}
Posted
Updated 17-Nov-16 23:17pm
Comments
Philippe Mori 18-Nov-16 10:22am    
now.Year - 100 is suspicious. Year in date time are "four digits". So we are in 2016 and not in 16 or 116...
Alex Sprint 20-Nov-16 23:25pm    
I agree but to check whether the byte[] conversion is correct i have to convert it back to DateTime. Hence i am seeking for an assistance.
Philippe Mori 20-Nov-16 23:46pm    
2016 - 100 would give 1916 which would overflow a byte. It serve no useful purpose to fix the conversion to BCD if the input is wrong. The problem would be even worst in BCD as BCD could only represent 100 numbers... In that case, it won't give a valid BCD number.
Alex Sprint 20-Nov-16 23:59pm    
Hi Philippe can you help me out on how to get the current DateTime and convert to 4 Byte[] ?

Well you have just to revert the process. Start with
C#
public static byte ConvertFromBcd(byte bcd)
 {
   return (byte) ((bcd >> 4) * 10 + (bcd & 0XF));
 }
 
Share this answer
 
The FromBinary method takes a long value that is created using the ToBinary method. It contains the Kind and Ticks components, and this is not what a database timestamp contains.

Using BitConverter to get the long value is correct, but then you have to take the time origin for the time stamp and add the long value as the correct unit. Assuming it's a timestamp from a MySQL database, IIRC it's the number of milliseconds from 1980-01-01:

C#
//Convert DateTime to bytes (always convert datetime to ticks)
byte[] byteValue = BitConverter.GetBytes(DateTime.Now.Ticks);
//Convert datetime to longvalue
long longVar = BitConverter.ToInt64(byteValue, 0);
//Convert to datetime.
DateTime dateTimeVar = new DateTime(longVar);
 
Share this answer
 
v2
Comments
Alex Sprint 20-Nov-16 23:28pm    
Hey puneet I am converting whole DateTime.Now to byte[] i need to convert it back to DateTime.
Er. Puneet Goel 21-Nov-16 0:34am    
//Convert DateTime to bytes (always convert datetime to ticks)
byte[] byteValue = BitConverter.GetBytes(DateTime.Now.Ticks);
//Convert datetime to longvalue
long longVar = BitConverter.ToInt64(byteValue, 0);
//Convert to datetime.
DateTime dateTimeVar = new DateTime(longVar);
Alex Sprint 21-Nov-16 1:46am    
Hi puneet Thanks for your suggestion.
How to mark this as solution ?
Er. Puneet Goel 21-Nov-16 4:11am    
Dear, Just accept the the Solution 1 above as solution. I have just updated solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900