Bits To Bytes

  • Thread starter Thread starter Nate.Strack
  • Start date Start date
N

Nate.Strack

I have a bool array representing the 32 bit dos datetime format and i
need to translate those 32 bits into 4 bytes so i can save.
 
I have a bool array representing the 32 bit dos datetime format and i
need to translate those 32 bits into 4 bytes so i can save.

Have you looked at BitArray and its CopyTo method?
 
i was able to get the copyto method to work but there has got to be a
better way to do this any ideas?
public byte[] DOS_TIMESTAMP
{
get
{
DateTime dt = DateTime.Now;
string sec = Convert.ToString(6,2).PadLeft(5,'0');
string min = Convert.ToString(37, 2).PadRight(6, '0');
string hrs = Convert.ToString(23, 2).PadRight(5, '0');

string day = Convert.ToString(dt.Day - 1,
2).PadRight(5,'0');
string mon = Convert.ToString(dt.Month,
2).PadRight(4,'0');
string year = Convert.ToString(dt.Year - 1980,
2).PadRight(7,'0');
string date = day + mon + year;
string time = hrs + min + sec;
string dtstamp = time.Substring(8, 8) + " " +
time.Substring(0, 8) + " " +
Reverse(date.Substring(0, 8)) + " " +
Reverse(date.Substring(8, 8));
bool[] dtarr = new bool[32];
for(int i = 0;i<32;i++)
{
dtarr = dtstamp.ToString() == "1" ? true :
false;
}
BitArray[] ba = new BitArray[4];
ba[0] = new BitArray(new bool[8] { dtarr[0], dtarr[1],
dtarr[2], dtarr[3], dtarr[4], dtarr[5], dtarr[6], dtarr[7]});
ba[1] = new BitArray(new bool[8] { dtarr[8] ,dtarr[9],
dtarr[10], dtarr[11], dtarr[12], dtarr[13], dtarr[14], dtarr[15] });
ba[2] = new BitArray(new bool[8] { dtarr[16],dtarr[17],
dtarr[18], dtarr[19], dtarr[20], dtarr[21], dtarr[22], dtarr[23] });
ba[3] = new BitArray(new bool[8] { dtarr[24],dtarr[25],
dtarr[26], dtarr[27], dtarr[28], dtarr[29], dtarr[30], dtarr[31]});

byte[] barrdt1 = new byte[1];
byte[] barrdt2 = new byte[1];
byte[] barrdt3 = new byte[1];
byte[] barrdt4 = new byte[1];

ba[0].CopyTo(barrdt1, 0);
ba[1].CopyTo(barrdt2, 0);
ba[2].CopyTo(barrdt3, 0);
ba[3].CopyTo(barrdt4, 0);

byte[] barrdt = new byte[4];

barrdt[0] = barrdt1[0];
barrdt[1] = barrdt2[0];
barrdt[2] = barrdt3[0];
barrdt[3] = barrdt4[0];

return barrdt;
}
}
 
This is code to get a DOS 4 byte timestamp for winzip if there is any
other shorter way please let me know.
public byte[] DOS_TIMESTAMP
{
get
{
DateTime dt = DateTime.Now;
string sec = Convert.ToString(dt.Second /
2,2).PadLeft(5,'0');
string min = Convert.ToString(dt.Minute, 2).PadLeft(6,
'0');
string hrs = Convert.ToString(dt.Hour, 2).PadRight(5,
'0');

string day = Convert.ToString(dt.Day,
2).PadRight(5,'0');
string mon = Convert.ToString(dt.Month,
2).PadRight(4,'0');
string year = Convert.ToString(dt.Year - 1980,
2).PadRight(7,'0');
string date = day + mon + year;
string time = hrs + min + sec;
string dtstamp = Reverse(time.Substring(8, 8)) +
Reverse(time.Substring(0, 8)) +
date.Substring(0, 8) + date.Substring(8, 8);
bool[] dtarr = new bool[32];
for(int i = 0;i<32;i++)
{
dtarr = dtstamp.ToString() == "1" ? true :
false;
}
BitArray[] ba = new BitArray[4];
ba[0] = new BitArray(new bool[8] { dtarr[0], dtarr[1],
dtarr[2], dtarr[3], dtarr[4], dtarr[5], dtarr[6], dtarr[7]});
ba[1] = new BitArray(new bool[8] { dtarr[8] ,dtarr[9],
dtarr[10], dtarr[11], dtarr[12], dtarr[13], dtarr[14], dtarr[15] });
ba[2] = new BitArray(new bool[8] { dtarr[16],dtarr[17],
dtarr[18], dtarr[19], dtarr[20], dtarr[21], dtarr[22], dtarr[23] });
ba[3] = new BitArray(new bool[8] { dtarr[24],dtarr[25],
dtarr[26], dtarr[27], dtarr[28], dtarr[29], dtarr[30], dtarr[31]});

byte[] barrdt1 = new byte[1];
byte[] barrdt2 = new byte[1];
byte[] barrdt3 = new byte[1];
byte[] barrdt4 = new byte[1];

ba[0].CopyTo(barrdt1, 0);
ba[1].CopyTo(barrdt2, 0);
ba[2].CopyTo(barrdt3, 0);
ba[3].CopyTo(barrdt4, 0);

byte[] barrdt = new byte[4];

barrdt[0] = barrdt1[0];
barrdt[1] = barrdt2[0];
barrdt[2] = barrdt3[0];
barrdt[3] = barrdt4[0];

return barrdt;
}
}
 
This is code to get a DOS 4 byte timestamp for winzip if there is any
other shorter way please let me know.

There is bound to be - converting the date and time into a binary
string and then into booleans is definitely not the way to go.

If you could provide details of the DOS timestamp format, that would
help enormously.

Jon
 
This is code to get a DOS 4 byte timestamp for winzip if there is any
other shorter way please let me know.

The bit-shift operators << and >> are good for lining integers up in the
correct places.
 
Back
Top