473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
3 10351
Plater
7,872 Expert 4TB
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
Burdzy
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
7,872 Expert 4TB
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

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

Similar topics

5
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
2
by: jiangyh | last post by:
hi there : I have a question about how to convert Type to DbType? thanks a lot. jiangyh
1
by: Islam Elkhayat | last post by:
In my C# Webapplication.. I enter the date in a textbox using popup Calender in the format MM/dd/yyyy.. when i retrieve date from Sql server for updating using datarow to fill the textbox the...
3
by: David | last post by:
I need to covert DateTime Variables into a byte array but all of the conversion methods do not appear to handle the DateTime type. Is there a generic way to convert any property or object into a...
5
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
3
by: Omer kamal | last post by:
Dear all, I want to save my data into MS Access database Table which consist of some fields with short date and short time formats. I try to save data in to the said table and I am getting error...
1
by: John | last post by:
Hi I am using and access datetime field to store both date & time. How can I split it into date & time after reading the field and then combine the date & time back into datetime when storing...
2
by: kirke | last post by:
Hi, I have a datetime column named dtDateTime. its format is "Oct 27 2006 12:00:00 " I want to group by only date part of it and count my code is $sql1="SELECT ...
4
by: Terry | last post by:
I have a TextBox with a date such as 15/01/2006 which I want to cast into a variable as a short date 15/01/06, also I need to cast a time such as 07:30 A.M. into a variable as a short time. What...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.