By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

C# Program: Convert 2 Byte Time & 2 Byte Date to DateTime

P: 2
Hi, I am working on writing a .zip file class based on the PKWare Specifications, and in the .zip format, they save the modified time of the file in 2 pieces:
1. 2 bytes for the time
2. 2 bytes for the date.

I am trying to convert that to a C# DateTime datatype, but I can't seem to find any documentation on how to do that. Has anyone done this, and/or show me where to find some documentation on this??

Thanks in Advance!!
-Burdzy

P.S. Here is some Sample Data:
Time:
byte[0] = 91
byte[1] = 161

Date:
byte[2] = 185
byte[3] = 54

Those 4 bytes should be 8:10 PM 5/25/2007
Jun 12 '07 #1
Share this Question
Share on Google+
3 Replies


Plater
Expert 5K+
P: 7,872
Is there anyway that you can combine them into a single number?
DateTime has various static methods for converting file times and other times to the object.

Also, are you aware that the .net framework has a compression algorithm included?

The Tick values for "8:10 PM 5/25/2007" as byte[8]:
[0] 0
[1] 220
[2] 155
[3] 206
[4] 31
[5] 109
[6] 201
[7] 8
Jun 12 '07 #2

P: 2
Hi Plater,

Thanks for the reply. Are you referring to the System.IO.Compression Class? I believe that only does the Compression/Decompression of the datastream, but does not give you full access to a .zip file such as what files are contained in it, etc.?? That part is what I am trying to implement.



I Found the specifications for the file format that I was working with here: http://msdn2.microsoft.com/en-us/library/ms724247.aspx



From that, I was able to write the following:

Expand|Select|Wrap|Line Numbers
  1.  
  2. private void button1_Click(object sender, EventArgs e)
  3.  
  4. {
  5.  
  6. byte[] bTimeDate = { 91, 161, 185, 54 };
  7.  
  8. byte[] bTime = {91, 161};
  9.  
  10. byte[] bDate = {185, 54};
  11.  
  12. MessageBox.Show(GetDateTimeFromDosDateTime(ByteToInt16(bTime), ByteToInt16(bDate)).ToString());
  13.  
  14. MessageBox.Show(GetDateTimeFromDosDateTime(ByteToInt32(bTimeDate)).ToString());
  15.  
  16. }
  17.  
  18. public static DateTime GetDateTimeFromDosDateTime(Int32 i32TimeDate)
  19.  
  20. {
  21.  
  22. Int16 i16Time = (Int16)(i32TimeDate & 0xFFFF);
  23.  
  24. Int16 i16Date = (Int16)((i32TimeDate & 0xFFFF0000) >> 16);
  25.  
  26. return GetDateTimeFromDosDateTime(i16Time, i16Date);
  27.  
  28. }
  29.  
  30. public static DateTime GetDateTimeFromDosDateTime(Int16 i16Time, Int16 i16Date)
  31.  
  32. {
  33.  
  34. int iYear = 0;
  35.  
  36. int iMonth = 1;
  37.  
  38. int iDay = 1;
  39.  
  40. int iHour = 0;
  41.  
  42. int iMinute = 0;
  43.  
  44. int iSecond = 0;
  45.  
  46. iDay = (i16Date & 0x1F);
  47.  
  48. iMonth = ((i16Date & 0x01E0) >> 5);
  49.  
  50. iYear = 1980 + ((i16Date & 0xFE00) >> 9);
  51.  
  52. iSecond = (i16Time & 0x1F) * 2;
  53.  
  54. iMinute = ((i16Time & 0x07E0) >> 5);
  55.  
  56. iHour = ((i16Time & 0x0F800) >> 11);
  57.  
  58. return new DateTime(iYear, iMonth, iDay, iHour, iMinute, iSecond);
  59.  
  60. }
  61.  
  62. public static Int32 ByteToInt32(byte byte0, byte byte1, byte byte2, byte byte3)
  63.  
  64. {
  65.  
  66. return byte0 + byte1 * 256 + byte2 * 256 * 256 + byte3 * 256 * 256 * 256;
  67.  
  68. }
  69.  
  70. public static Int32 ByteToInt32(byte[] convertMe)
  71.  
  72. {
  73.  
  74. return convertMe[0] + convertMe[1] * 256 + convertMe[2] * 256 * 256 + convertMe[3] * 256 * 256 * 256;
  75.  
  76. }
  77.  
  78. public static Int16 ByteToInt16(byte[] convertMe)
  79.  
  80. {
  81.  
  82. return (short)(convertMe[0] + convertMe[1] * 256);
  83.  
  84. }
  85.  
  86. public static Int16 ByteToInt16(byte byte0, byte byte1)
  87.  
  88. {
  89.  
  90. return (short)(byte0 + byte1 * 256);
  91.  
  92. }
  93.  
Jun 12 '07 #3

Plater
Expert 5K+
P: 7,872
Looks like it worked, have you tried the BitConverter class? It turns bytes into values for you

i also did the following:
Expand|Select|Wrap|Line Numbers
  1. byte[] bdate = new byte[] { 185, 54 };
  2. UInt32 date = BitConverter.ToUInt16(bdate, 0);
  3. UInt32 day = PullPiece(date, 0, 4);// 2^0 -2^4 bits (5bits) make up the day
  4. UInt32 month = PullPiece(date, 5, 8);
  5. UInt32 year =1980+ PullPiece(date, 9, 15);
  6.  
  7.  
Expand|Select|Wrap|Line Numbers
  1. public UInt32 PullPiece(UInt32 buffer, int startbit, int endbit)
  2.         {
  3.             UInt32 retval = 0;
  4.             BitArray ender = new BitArray(32);
  5.             BitArray ba = new BitArray(new Int32[] { (Int32)buffer });
  6.             int counter=0;
  7.             for (int i = startbit; i <= endbit; i++)
  8.             {
  9.                 ender[counter] = ba[i];
  10.                 counter++;
  11.             }
  12.             byte[] interm = new byte[4];
  13.             ender.CopyTo(interm, 0);
  14.             retval = BitConverter.ToUInt32(interm, 0);
  15.             return retval;
  16.         }
  17.  
Jun 13 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.