473,385 Members | 1,630 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,385 software developers and data experts.

Bits To Bytes

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.

Jan 7 '07 #1
5 1498
<Na*********@gmail.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 7 '07 #2
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[i] = dtstamp[i].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;
}
}

Jan 7 '07 #3
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[i] = dtstamp[i].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;
}
}

Jan 8 '07 #4
Na*********@gmail.com wrote:
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

Jan 8 '07 #5

<Na*********@gmail.comwrote in message
news:11*********************@51g2000cwl.googlegrou ps.com...
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.
Jan 8 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zulik | last post by:
Hello, I have a problem with MessageDigest MD5 in Java. I want to calculate the digest from a file, encode it in base64 and display it. Now, according to MD5 spec, digest shall be 128 bits so,...
10
by: Eric | last post by:
I have an array that contains over 30000+ bits. The size varies at runtime. I need to move through this chunk of memory and count bits every so often. Example: First 9 bits has 2 ones, next 10...
8
by: Sowen | last post by:
Hi, all I am wondering how to write bits by using ofstream? I have finished a huffman tree, but how can I write the bits to the file in order to gain compression? for example, 'A' returns a...
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
9
by: John | last post by:
Hi, I need to write out bits that I receive from another process. These are boolean values. I need there to be 8 bits in every byte. I know I could write these bits out as char's using one bit...
2
by: Pete | last post by:
Hi, First, thanks for any time you spend helping me, I'm at a loss. I'm not bit-savvy, so I apologize if this is extremely simple, or I am going about this the wrong way. I am trying to take a...
25
by: Daniel Kraft | last post by:
Hi, I do need to implement something similar to C++'s std::bitset in C; for this, I use an array of int's to get together any desired number of bits, possibly larger than 32/64 or anything like...
59
by: riva | last post by:
I am developing a compression program. Is there any way to write a data to file in the form of bits, like write bit 0 then bit 1 and then bit 1 and so on ....
4
by: rkmr.em | last post by:
hi i want to send unsigned 32 bit integer to socket, and looking for something equivalent to this method... http://livedocs.adobe.com/flex/2/langref/flash/net/Socket.html#writeUnsignedInt() ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.